
0x0F stuff
Hello! I often do research and explore source files from other programs. More often, I see strange pieces of code, like:
Code:
if ( (m5 & 0xF0) == 0) {
byte b = m5 & 0xF;
I am really confused... my ambitions are to become a good programmer, so I have to know all about this... hex and binary stuff, if someone knows some good links to tutorials or articles regarding to this, please post them here. I wasn't born in the 70s, when you had to know only lowlevel programming languages, so I don't know them.

Most C programming books have a section on hexadecimal and bitwise operators.

It is simple: & is the bitwise AND operator (do you know about boolean?). If m5 has a
bit pattern of 00010100 in memory(2 powered 4 + 2 powered by 2) and 0xF=(15) has a
bit pattern of 00001111 then b will have a
bit pattern of 00000100 that is equal to 4.
[edit]
In this way we can ignore the left 4 bits of m5.

In other words, the resulting bit will only be 1 if both of the input bits are 1.
You can find information like this in truth tables. They look like this:
Code:
AND (&)
in1 in2 out
0 0 0
1 0 0
0 1 0
1 1 1
OR ()
in1 in2 out
0 0 0
1 0 1
0 1 1
1 1 1
...

To convert hexadecimal to decimal:
Hexadecimal digits are:
1 2 ... 9 A=10 B=11 C=12 D=13 E=14 F=15
When Code:
4*3 = 3 multiplied by 4 = 12
4^3 = 4 powered by 3 = 2^6 = 64
(3*(5^2 )) = 5 powered by 2 then the result is multiplied by 3 = 3*25 = 75
0 multiplied by any number is zero. So for example 0*(16^2) = 0
Then we convert hexadecimal to decimal like this: Code:
0x0024AD3= (3*(16^0))+ (13*(16^1))+(10*(16^2))+(4*(16^3))+(2*(16^4))+(0*(16^5))+...

Thanks, a bit clearer about the operators, but when do we use hexadecimal values and why? What's the purpose of them? Examples and links appreciated...

Code:
F = (2^3)+(2^2)+(2^1)+(2^0) = 1111
E = (2^3)+(2^2)+(2^1) = 1110
D = (2^3)+(2^2)+(2^0) = 1101
C = (2^3)+(2^2) = 1100
B = (2^3)+(2^1)+(2^0) = 1011
A = (2^3)+(2^1) = 1010
9 = (2^3)+(2^0) = 1001
8 = 2^3 = 1000
7 = (2^2)+(2^1)+(2^0) = 0111
6 = (2^2)+(2^1) = 0110
5 = (2^2)+(2^0) = 0101
4 = (2^2) = 0100
3 = (2^2)+(2^0) = 0011
2 = (2^1) = 0010
1 = (2^0) = 0001
0 = 0000
Now it is so easy to convert from hexadecimal to binary. Each hexadecimal digit represents four binary digits.
Code:
0xAA = 1010 1010
0xA4 = 1010 0100
0xFD4A = 1111 1101 0100 1010
0xABCDE = 1010 1011 1100 1101 1110
Hexadecimal is easier to work with. Now try to write binary of 0xFF7028A4 :)

Hex is useful because it maintains a closer relationship to binary than decimals,
so it is very, very easy to convert a long hex number to binary (and vice versa).
Example: if you were told to write this binary number in hex, you could do it in
your head, whereas it'd be more difficult to convert to decimal:
11101001101100101111
Step 1: break the number into 4 bit pieces (if the number of bits isn't divisible
by 4, add leading 0's  i.e. put extra 0's at the left of the number):
1110 1001 1011 0010 1111
Step 2: convert each 4 bit piece to hex (which is quite easy if you can count in
binary already). Here's how you might appraoch it:
1110  this is 14 in decimal  which means it's E in hex (either look it up or you
learn the hex digits off!)
1001  this is 9 in decimal  same in hex
1011  this becomes 11 in decimal, so it must be B
0010  2 in decimal, 2 in hex
1111  this is 15  hex has digits for numbers 015, so the last digit of the hex
system is 15, which is F.
Step 3: take the digits you just converted and string them together in the same
order as they were in the first place  so the number is written simply as:
E9B2F
To convert from hex to binary, simply reverse the proceedure  write each hex
digit as its binary equivalent and stick it together.
There's also a number system called octal, which is base 8  it has the same
features as hex, only digits 0  7 are used so there's no need to worry about
letters. You can do conversions with octal in the same way as hex, only you
break the binary number into 3 bit pieces. There's a bad joke that goes as
follows:
Programmers (or some other binary educated group ;) ), can't tell the difference between
halloween and christmas because
Dec 25 == Oct 31
Convert octal 31 to decimal and you'll "get" it.

Ah, cool, got it. Thank you very much for the brief explanation, now I understand. One last thing how do I use hex in my program? I can cout<< them and use bitwise operations, but still, I don't get, where it is useful/needed to use hex instead of normal numbers. A simple example would be helpful.

For bit manipulation it is often easier to remember the hex (once you are used to it) for a bit than it is to remember the decimal for a bit... and it is easier to remember (i & 0xF) to get the lowest nibble than to remember (i & 15)... atleast for me it is.

Great thing about binary, you only really need to know the first 15.
Also notice this
1 = 0001
2 = 0010
4 = 0100
8 = 1000
3 = 0011
6 = 0110
12 = 1100
5 = 0101
10 = 1010
bit shifting left = multiplication by 2.

that's a nice trick! A lot easier to learn.