How bit representation of a number can be done in c?

I have an assignment in which a character is the input of which some bits(from a position to certain position) are to be inverted (1's complement) and then the resultant character is to be returned....for example

unsigned char x = J      from p = 3  to offset n = 5

01001010 inverted to 01110100

't' is the output

Thanks

This looks like homework to me, but I'll give you a start. Bit inversion is usually taken to mean reversing the order of bits, BTW. "flip" means change from 1->0 or 0->1.

Use a bit field to see what is set, or you can use any of dozens of bit twiddles - which I suspect is what your teacher wants you to do.

See:

Bit Twiddling Hacks

Note: if this is homework , please post in the correct forum - some other moderator may close this thread otherwise.

1 Like