# setting the values of bits

• 04-11-2008
yahn
setting the values of bits
say I have two chars and I want to mix up some of the bits in the chars. How exactly do I do this?

Code:

```1110 0101 0011 1101```
and I wanted to switch the first bits of the chars to get:
Code:

```0110 0101 1011 1101```
How would I code that?

I know there is something that I'm forgetting and when someone tells me I'll kick myself, but right now I think I'm making this way harder than it is. Thank you.
• 04-11-2008
Vicious
You could use bitwise OR

Code:

```int flag = 0x80; // 1000 0000 int value = 8; // 0000 1000 value = value | flag; // value == 1000 1000```
• 04-11-2008
CornedBee
There's the bitwise logical operators that you can use for such games. But none of them allow you to switch individual bits of two patterns - that requires a more complicated expression.
• 04-11-2008
Vicious

You could do something similar thoguh. If you just wanted the "first" bit then you could test it with and.

Code:

```unsigned char byte1 = 0x08; // 0000 1000 unsigned char byte2 = 0x80; // 1000 0000 // if first bit is 1 make a flag with that bit as 1 also or else 0 unsigned char byte1_flag = (byte1 & 0x80) ? 0x80 : 0x00; unsigned char byte2_flag = (byte2 & 0x80) ? 0x80 : 0x00; // "swap" the bits byte1 |= byte2_flag; byte2 |= byte1_flag;```
I think something like that would work. I just typed it in the quick reply so I haven't tested it.

Heh, apparently I forget my logic. The byte2 "swap" will be 1 OR 0 and will leave it as 1 not change to 0. Shouldn't require too much change though.
• 04-11-2008
brewbuck
Code:

```if(v1 & 0x80 ^ v2 & 0x80) {     v1 ^= 0x80;     v2 ^= 0x80; }```
• 04-11-2008
master5001
Do you mean setting arbitrary bits?

Code:

```#include <iostream> void setBit(int &input, int bit) {   input |= 1 << bit;  // OR in 1 moved over bit times } // conversely void unsetBit(int &input, int bit) {   input &= ~(1 << bit);  // heavy on the syntax here. This just means                                     // keep every bit except for 1 moved over bit times } // usage: int main(void) {   int x = 0x80;   setBit(x, 3);   unsetBit(x, 8);   std::cout << x;   return 0; }```
Is that what you meant?
• 04-11-2008
yahn
master I think that is what I want. I'm trying to figure out the fastest way to create a matrix like block and shift the bits around.

I want to use a block of 8 bytes. I want to set the first byte to the 1st bit of the 1st byte to the 1st byte of the second byte and then the 2nd bit of the 1st byte to the 2nd bit of the 2nd byte... then the 2nd byte I want to set the 1st bit of 2nd byte to the 1st bit of the 2nd bit then the 2nd bit of the 2nd byte to the 2nd bit of the 3rd byte... etc

I created this loop:
Code:

```    for (i = 0; i < 8; i++)     {         for (int j = 0; j < 8; j++)         {             block[i] |= str[(j + i)%8] & (2 << (7 - j));         }     }```
But the result seems to be pretty far from what I want. I'm obviously doing something wrong, but I can't find it. Can someone help. Thank you.
• 04-11-2008
master5001
Cool. Well 8 bytes is 64-bits, right? So instead of messing with arrays why not just use a single long long, which is 8 bytes.
• 04-11-2008
master5001
Example
Code:

```void setMatrixBit(long long &input, int x, int y) {   input |= 1 << (x + (y * 8)); }```
 I may as well put an unset too :)
Code:

```void unsetMatrixBit(long long &input, int x, int y) {   input &= ~(1 << (x + (y * 8)); }```
Not to be too scary with optimizations, but (x + (y << 3)) would yield a slightly faster yet identical result. x and y denote where in the matrix you are trying to set a bit (my code assumes an 8 x 8 matrix.[/edit]
• 04-11-2008
CornedBee
Quote:

but (x + (y << 3)) would yield a slightly faster yet identical result
That's assuming your compiler is so stupid that it doesn't do that anyway. Very unlikely.
• 04-11-2008
master5001
How dare you judge my compiler so harshley! Seriously though, true dat. Not to mention the fact I also got waaaaaay ahead of myself and just assumed your matrix is 8x8. Obviously you can change the 8 to whatever the width of your array may be.
• 04-11-2008
DougDbug
yahn,

Take a look at the Bitwise Operation FAQ

You may have noticed that some posters have used hexadecimal (i.e. 0x80) in their examples/replies.We usually use hex when doing bit manipulation.

You can't directly* use binary in C/C++, and this makes it very difficult for beginners, because you can't tell that you've changed bit 7** by looking at the decimal values... you have to do some calculations***. (Most of the time, wouldn't want to use binary directly, because binary numbers can be difficult to read or write, especially when you get beyond 16 bits!)

Converting between hex and binary is much easier than converting between decimal and binary.

It takes a bit of study and practice if you are not used to working with binary and/or hex, but you can learn to convert numbers (of any size) in your head. This is because each nybble (4-bits) converts exactly to one hex-digit. Once you memorize 16 conversions (0-F), you can do binary to hex conversions (and vice-versa) without any calculations!

In your example, only the high-nybble is affected by changing bit 7. Notice that "5" remains "5" and the "D" remains "D".

1110 0101 = 229 decimal = E5 hex
0110 0101 binary = 101 decimal = 65 hex

0011 1101 = 61 decimal = 3D hex
1011 1101 binary = 189 decimal = BD hex

Every time you see 0101, it converts to 5 hex, etc... If you know that 1110 0101 = E5 hex, then you know that 0101 1110 = 5E hex.... Much easier than decimal!

* Decimal, hex and octal are included in cin and cout, and you can directly use these "bases" in your code. Binary is not included in cin and cout, and you cannot use it directly in your code source. (C++ does include the <bitset> container, which makes it fairly easy to display numbers in binary.)

** The bits (in a byte) are numbered 0 through 7, where bit-0 is the least significant bit.

*** The calculator included with Windows can do binary, octal, decimal, and hex conversions if you click on Scientific.
• 04-13-2008
yahn
I see how the long long would be much more efficient, but I don't really see how it works. Also I don't see what I'm doing wrong in the loop.

str[(j + i)%8] seems like it should be getting the correct char in the array and (1 << (7 - j)) seems like it should be shifting to the right place but I'm thinking that I may need to add another shift to the end because it seems like it might only work for the first byte.

What exactly is wrong with the loop?
• 04-13-2008
Dave_Sinkula
• 04-14-2008
master5001
Well I am definitely going to need a little bigger snippet of code to help you fix your loop. Since given what you posted I can only guess as to what the result is supposed to be doing. So yahn go ahead and either better explain what the loop is supposed to do, or just give me the whole function.