Understanding bit manipulation (set/clear) in C programming

I'm stuck understanding bit operations on integers in C. Suppose I have the number 13. Its binary representation is 1101 . How can I set the bit at its second position? How can I clear the bit? Here is the function I wrote so far for setting the bit:

int setBit(int data, int pos, int val)
Will this work correctly? 10.8k 13 13 gold badges 82 82 silver badges 139 139 bronze badges asked Mar 21, 2015 at 17:20 3,531 2 2 gold badges 23 23 silver badges 35 35 bronze badges

xor ^ is used to toggle the bit, not clear. And even after fixing that, your program still works incorrectly for val = 2 or anything nonzero and not 1, which is also considered true

Commented Mar 22, 2015 at 3:30

Can you please be more precise: add extra bit at second position and remove at specific position are confusing expressions. Do you want to set the 0 bit in 1101 , and later clear or reset a bit given its position numbered from the least significant one?

Commented Mar 22, 2015 at 7:38 I dont understand the negative votes on this question Commented Mar 10, 2018 at 21:19

5 Answers 5

How does it work?

Result is 000..0101.

For inserting a bit y at position x:(position starts from 0)

1101---->11y01 Giving the example for position 2.

num= FFFF FFFF (in hex)(all 1's) //1111. 1111 number=N // in which you will insert bit num1=num 

Now set the x-th bit(here x=2) as you required using the method described below

Note: More generally changing the kth bit of number n to y (maybe 0 or 1) can be done this way

n^=(-y ^ n) & (1U < 

Deletion of a bit is similar to insertion. Step by step perform the operation and you will get it.

EDIT: I have changed the use of 1 to 1U because in first case when using only 1 without any modifiers is defined to be an signed int. From K&R the right shifts of signed values are implementation defined. Also if you left-shift a signed number so that the sign bit is affected, the result is undefined.

These operations on unsigned value have well define behaviour: Vacated fields are filled with zeroes.

answered Mar 21, 2015 at 17:24 user2736738 user2736738 30.9k 5 5 gold badges 43 43 silver badges 56 56 bronze badges but i want to insert bit in at third position so output like 11011 Commented Mar 21, 2015 at 17:40

for inserting i need insert code setBit code i have and it work fine but inserting is not possible with this..i hope You understand what i try to do

Commented Mar 21, 2015 at 17:42 so how can i perform insertion Commented Mar 21, 2015 at 17:43 sorry typo mistake done there i update it ya i want like this Commented Mar 21, 2015 at 17:44 Now You got it what i want Commented Mar 21, 2015 at 17:47

Setting, clearing and toggling the state of a bit is straightforward:

inline void bit_set (unsigned long *bf, unsigned char n) < *bf |= (1 inline void bit_clear (unsigned long *bf, unsigned char n) < *bf &= ~(1 inline void bit_toggle (unsigned long *bf, unsigned char n)

Note: bitfields, and the functions above, are zero based (i.e. the least significant bit is bit 0 not bit 1 ) So if you want to clear, set or toggle the second bit from the right ( bit index 1 , the 2's bit (binary) , or bit 2 counting right-to-left), you pass a bit index of 1 . n in the functions above is the bit index . The following is a quick reference:

 +-----+-----+-----+-----+-----+-----+-----+-----+ bit index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | +-----+-----+-----+-----+-----+-----+-----+-----+ binary | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | +-----+-----+-----+-----+-----+-----+-----+-----+ 

Here is a quick example of the use operating on bit 1 , (the 2's bit in binary):

#include #include #define WDSZ 64 /* bit functions */ inline void bit_set (unsigned long *bf, unsigned char n) < *bf |= (1 inline void bit_clear (unsigned long *bf, unsigned char n) < *bf &= ~(1 inline void bit_toggle (unsigned long *bf, unsigned char n) < *bf ^= (1 /* simple return of binary string */ char *binstr (unsigned long n); int main (int argc, char **argv) < unsigned long bf = (argc >1) ? strtoul (argv[1], NULL, 10) : 13; printf ("\n original value : %3lu (%s)\n", bf, binstr (bf)); bit_set (&bf, 1); printf (" set bit 1 : %3lu (%s)\n", bf, binstr (bf)); bit_clear (&bf, 1); printf (" clear bit 1 : %3lu (%s)\n", bf, binstr (bf)); bit_toggle (&bf, 1); printf (" toggle bit 1 : %3lu (%s)\n\n", bf, binstr (bf)); return 0; > /* simple return of binary string */ char *binstr (unsigned long n) < static char s[WDSZ + 1] = ; char *p = s + WDSZ; while (n) < p--; *p = (n & 1) ? '1' : '0'; n >>= 1; > return p; > 

Output

$ ./bin/bitsetcleartoggle original value : 13 (1101) set bit 1 : 15 (1111) clear bit 1 : 13 (1101) toggle bit 1 : 15 (1111)