# quick question about bit manipulation

• 01-09-2003
PJYelton
I'm pretty new to bit manipulators and I have a quick question. Say I've got a byte set to something like "10110101" and I want to set the the 3rd bit to 0. I know I need to XAND it with the byte "11011111" but I'm not sure of the easiest way to do this in a program. I can't just write:
Code:

```// x is equal to an arbitrary byte and I want to set the third bit to zero x &= 11011111;```
can I? Is there an easy way to write it simply like this or do I need to calculate out what the base-10 value of 11011111 is and instead XAND it with that? Basically I want to end up writing a function that will set a particular specified bit to zero.
• 01-09-2003
PJYelton
Duh, didn't think about using the XOR operator, thats a much simpler process. Thanks, I think I will use macros, should be much easier.
• 01-09-2003
DougDbug
I do it exactly like you, PJYelton. This makes your code very readable, although I usually use hex format which is simpler to type, but not as readable for those who don't know how to do hex to binary conversions in their heads.

I hope I don't screw-up the logic here... But These are the things I do quite frequently:

AND with ONE in test position(s) to test if a bit is one. (i.e. false if bit is zero, true if one.)

AND with ZERO to force to zero

OR with ONE to force to one

I often use the invert operator '~' if I need "set" and "clear" the same bits repetedly. (i.e. if under one set of conditions I need to set bits 1 and 3 to zero, and later set bits 1 and 3 to one.)

I don't think I've used XOR to "flip a bit" because I usually have to just read or write a particular bit, or I need to confirm it's state before inverting it.

I've been burned by the size-of problem too! (But I'm usually working on non-portable hardware-specific code when I'm manipulating bits.) I think it would be acceptable to use use "size-of" to generate your mask.

As far as I know a byte is always 8 bits... at least in my (hardware) world. There is no "byte" type in C++.

Finally... bits are usually identified by counting left from the LSB (Least Significant Bit) starting with zero (kinda like arrays). So a byte contains bits 0 thu 7. What you're calling the "third" bit is "bit 5" (and it has a "weight" of 32). Sometimes we might call it "the 32 bit", but I don't think it's technically correct... maybe that's engineering slang.
• 01-09-2003
Nick
Quote:

I'm pretty new to bit manipulators and I have a quick question. Say I've got a byte set to something like "10110101" and I want to set the the 3rd bit to 0. I know I need to XAND it with the byte "11011111" but I'm not sure of the easiest way to do this in a program. I can't just write:
This is actually bit 6 or bit 5 if you count by 0.
I would recommend having a group
have to set a bit you would do x |= mask[5] and to clear

Bytes are not always 8 bits, there are a few machines that
don't have char's having 8 bits like
the Cray super computer. But because
there are so few machines and bit manipulation is usually
in level parts of the system I don't see anything wrong
with assuming bytes are 8 bits. You should make sure
that you use unsigned char. To do this
just typedef unsigned char uint_8.
• 01-09-2003
PJYelton
Thanks for all your help guys! While I've always understood binary numbers I just started to work with bit manipulation in programming about 3 days ago, and I got hit by a landslide of information to learn! :D
• 01-09-2003
Panopticon
Re: quick question about bit manipulation
Code:

`x&=223; //what you call the 3rd bit is the 5th bit.`
11011111 = 255-32 = 223

thats how i do it. i dont know what vVv is talking about there. I dont think its that complicated.. or did i miss something.
• 01-09-2003
PJYelton
Didn't say it was complicated, I was just wondering if there was another way to do it other than calculating out the base-10 value like you did.
• 01-10-2003
Nick
Code:

```#include <climits> ... #define THIRD_BIT( x ) ( 1 << ( ( CHAR_BIT * sizeof( x ) ) - 3 ) ) ... char x = 32; if( x & THIRD_BIT( x ) ) /* is third bit set? */   x ^= THIRD_BIT( x ); /* yes, clear it */```
There is something wrong with this right?
I think because it will do the subtraction and the shift using signed integer arithmetic. It probably won't work on all machines anyways.