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

What is wrong with this program statement?

0 Answers  


Write the syntax and purpose of a switch statement in C.

0 Answers   Adobe,


write a program for even numbers?

19 Answers   TCS,


How does normalization of huge pointer works?

0 Answers  


consider the following C code main() { int i=3,x; while(i>0) { x=func(i); i--; } int func(int n) { static sum=0; sum=sum+n; return(sum); } the final value of x is

4 Answers   TCS,






Explain how does free() know explain how much memory to release?

0 Answers  


print out of string in this format; 1. "rajesh" 2. \n 3. %d

5 Answers   mpower,


#define MAX(x,y) (x) >(y)?(x):(y) main() { inti=10,j=5,k=0; k= MAX(i++,++j); printf("%d..%d..%d",i,j,k); }

0 Answers   Wilco,


How do you declare a variable that will hold string values?

0 Answers  


string reverse using recursion

0 Answers   Mind Tree,


Explain what could possibly be the problem if a valid function name such as tolower() is being reported by the c compiler as undefined?

0 Answers  


what is an inline fuction??

2 Answers  


Categories