Hello i am currently reading C++ for dummies and i am stuck, cause i am a dummy.
I do not quite understand hexadecimals and how they are used in a program. It goes on to say that you use bitwise operators but i do not understand. Please help.
Hexadecimal numbers are very important in computers because of octet-sized byte. The trick is that when you change the base of a given number, the number of digits on which it is expressed changes, too! From base 2 (binary) to base 16 there is always a fixed conversion: 4 binary digits goes to one hexadecimal, 8 bin. -> 2 hex. This is useful especially when working with bits, and let me give an example why it would be important to work with bits: a "bool" value does hold only one bit of information (true/false), but the computer's memory can not be accessed at the bit-level. It has to be at least a byte, so for a single boolean bit, the computer allocates 8! The "usual" access to that boolean value means access to and usage of only one bit, but with bitwise operations we may access each of them.
Let's look and analyze for now half of that byte: 0b0001 <=> 0x1
0b0010 <=> 0x2
0b0100 <=> 0x4
0b1000 <=> 0x8
If I had to express that series in normal usual decimal number, I've had numbers ranging from one digit to three and a real headache trying to figure out which number means which binary bit (in real practice we're coming to learn them, but still). Thanks to the nature of hexadecimal numbers, and thanks to their limited use, we can note them with a fixed format and use them easily for binary arithmetics - each hex digit is the base 2 raised to power of the binary (0-based index) position bit.
The rest of the byte is not much of a difference: 0x00010000 <=> 0x10
0x00100000 <=> 0x20
. . .
Now, back to exploiting the other remaining bits in an octet...
I have the valuable bit-field B and I wish to read it's third bit in variable A. First, clear A:
A = 0;
second, use a bitwise operation - '&' or '|'. Now I have to remember what these bit operator means. 0&1 = 0; 0|1 = 1; The third bit in an 8-bit byte would be 0b00000100 which in hex is 0x04. When I make: A = B & 0x04;
this would take each bit of B and make '&' with it's corresponding bit from 0x04 bit-mask, and in the assignment result all the B's bits are nullified but the third which remain as it was. The result from the B & 0x04 operation could be either 0b00000100 or 0b00000000, which in hex would be 0x00 or 0x04. And that value is assigned to previously-cleared A.
If it would interested me the fifth bit, I had chosen another mask: 0b00010000 which is 0x10: A = B & 0x10;
If seven'th and four'th - 0b01001000 which would be 0x48 (remember the four-bit correspondences: 0b0100 <=> 0x4 and 0b1000 <=> 0x8, right?), so if I use this given mask: A = B & 0x48;
Then I've just read the custom-chosen 7'th and 4'th bits, that I might further use or custom-access toward another variable.
Hexadecimals are not just preferable or desirable, they make a carefully chosen solution. Even if I come to be able to read bits in a byte of 0-255 decimal range, when the number is rising, say over million, it is nothing but a pain. The hexadecimal are staying beautifully in groups of 0-F double-digits for each byte, wherever would be the memory length (object's size).