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

Write a c program to enter a string of paragraph and replacing a particular word which is repeated in the paragraph by another word?

2 Answers   ME, Synfusion, Wipro,


What is the meaning of ?

0 Answers  


What is the difference between malloc() and realloc()?

2 Answers  


What is the best style for code layout in c?

0 Answers  


prog for 1st five prime numbers in 2^x - 1

0 Answers  


What is break in c?

0 Answers  


Write a program to write a given string in maximum possibilities? i.e str[5]="reddy"; i.e we can write this string in 120 ways for that write a program

3 Answers   Subex,


count the numbers between 100 and 300, that star with 2 and ends with 2

5 Answers   Mind Tree,


without using arithmatic operator convert an intger variable x into x+1

3 Answers  


Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging?

0 Answers  


what is the coding of display the factorial of a number using array and function?

1 Answers  


What is the deal on sprintf_s return value?

0 Answers  


Categories