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 c system32 taskhostw exe?

0 Answers  


Write a c program to build a heap method using Pointer to function and pointer to structure ?

0 Answers   Wipro,


How to check whether string is a palindrome, WITHOUT USING STRING FUNCTIONS?

2 Answers   Aricent, Manipal University,


how can i get the string which is having two spaces at the end.suppose the string is "Hello World ".Now at the end i have two spaces.i need to print with that spaces .

1 Answers  


What is difference between structure and union?

0 Answers  






list the no of files created when c source file is compiled

9 Answers   TCS,


How can I trap or ignore keyboard interrupts like control-c?

0 Answers  


Explain what happens if you free a pointer twice?

0 Answers  


What is methods in c?

0 Answers  


#include <stdio.h> int main() { if ("X" <"x") printf("X smaller than x "); } my question is whats the mistake in this program? find it and please tell me..

3 Answers  


Is stack a keyword in c?

0 Answers  


Describe the header file and its usage in c programming?

0 Answers  


Categories