How we can set and clear bit in a byte using macro function?

Answers were Sorted based on User's Feedback



How we can set and clear bit in a byte using macro function?..

Answer / c.p.senthil

#define SETBIT(num,bitpos) (num|(1<<bitpos))
#define CLRBIT(num,bitpos) (num&(~(1<<bitpos)))

int a = 0x03; // a = 00000011b = 0x03(3)
SETBIT(a, 3); // a = 00001011b [3rd bit set] = 0x0b(11)

int b = 0x25; // b = 00100101b = 0x25(37)
CLRBIT(b, 2); // b = 00100001b [2nd bit clear] = 0x21(33)

Is This Answer Correct ?    27 Yes 1 No

How we can set and clear bit in a byte using macro function?..

Answer / sunitha

take i=5;
print its binary value

now for setting a bit
i=i>>5|1;
this will set 5th bit in binary value of 5

now for clearing a bit
i=i>>5 xor 1
this will clear 5th bit in binary value of 5

Is This Answer Correct ?    6 Yes 3 No

Post New Answer

More C Interview Questions

How can I avoid the abort, retry, fail messages?

0 Answers  


Write a client and server program in C language using UDP, where client program interact with the Server as given below: i) The client begins by sending a request to send a string of 8 characters or series of 7 numbers, the server sends back a characters or numbers as per the request of the client. ii) In case of series of 7 numbers: The client sends a multiplication of numbers, to the server. iii) In case of a string of 8 characters: The client sends a reverse order of string to the server.. iv) Server will send an acknowledgment to the client after receiving the correct answer

0 Answers   Ignou, Microsoft,


Write a Program to accept different goods with the number, price and date of purchase and display them

0 Answers   HDFC,


Explain what is a 'null pointer assignment' error? Explain what are bus errors, memory faults, and core dumps?

0 Answers  


What is the function of ceil(X) defined in math.h do? A)It returns the value rounded down to the next lower integer B)it returns the value rounded up to the next higher integer C)the Next Higher Value D)the next lower value

3 Answers   Accenture, Wipro,






what is an inline function?

2 Answers   TCS,


write the program to find multiplication of 2-D matrix??????????

1 Answers  


Difference between MAC vs. IP Addressing

0 Answers  


Struct(s) { int a; long b; } Union (u) {int a; long b; } Print sizeof(s)and sizeof(u) if sizeof(int)=4 and sizeof(long)=4

2 Answers   Mascot,


i=10,j=20 j=i,j?(i,j)?i:j:j print i,j

1 Answers   CSC,


Do pointers store the address of value or the actual value of a variable?

0 Answers   Fidelity,


what is the differnce between AF_INET and PF_INET?

5 Answers   Systems Plus, Wipro,


Categories