@Jackson Marie
you didn't get the bit operations at all.
Unfortunately they're not easy to explain. Look at this:
http://cplusplus.com/doc/tutorial/operators/
for what type of bit operators exists.
Ok, what does this do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
struct atom
{
unsigned char x : 4; // This are the lower 4 bits in a byte
unsigned char y : 4; // This are the higher 4 bits in the same byte
unsigned char z : 4; // This are the lower 4 bits in a second byte
// it is possible to use other types like unsigned int which would lead to [4] usable bytes
};
// You can use it as follows
atom a;
a.x = 5; // The code for this would be: byte value = 5 | (y & 0xf0)
// what would happen if a.x = 20? Well: it exceeds the 4 bit capacity and would overwrite y!
a.y = 5; // The code for this would be: byte value = (5 << 4) | x
// the byte value: 0x55
int b = a.x // resolves to: byte value & 0x0f
int c = a.y // resolves to: byte value >> 4
|
if you write the hex value 0xf0 as binary it would be 11110000
The point is that c/c++ knows 3 number system: decimal, hexadecimal, and (rarely used) octal.
But unfortunately
not binary. So if you want to manipulate a certain bit (like the third: 00000100) you have to convert it. Using the calculator: The third bit has the value 4.
The
|
does the following 1 | 0 = 1 (this is just 1 bit, but it can be use for multiple bits)
The
&
does the following 1 & 0 = 0
The
<<
shifts the bits (5 -> 101) x (four in this case) times left -> (bin: 1010000 = dec: 80 = hex: 50)
The
>>
shifts the bits x (four) times right
hope the explanation helped somewhat (and i didn't make a mistake somwhere).
I suggest that (both) you check that out and play with some values to get the feeling for that
Thist may help:
http://en.wikipedia.org/wiki/Bitwise_operation