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 does sizeof return c?

0 Answers  


1.Why do you call C is middle level language? 2.Why do you call C is userfriendly language.

2 Answers  


what is the disadvantage of using macros?

1 Answers   Wipro,


What is the advantage of a random access file?

0 Answers  


What is %lu in c?

0 Answers  






how can I convert a string to a number?

0 Answers  


why we use pointer in c

7 Answers   HCL, TCS,


what will be the out put. #include<stdio.h> void main() { printf("Output:"); printf(1+"vikashpatel"); }//output: ikashpatel

1 Answers   V2 Solutions,


Are enumerations really portable?

0 Answers  


Program to find the absolute value of given integer using Conditional Operators

6 Answers   N Tech,


Write a c program to print the even numbers followed by odd numbers in an array without using additional array

1 Answers   Tech Mahindra,


How can I call fortran?

0 Answers  


Categories