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 low-level programming languages, so I don't know them.

08-14-2006

dwks

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

08-14-2006

siavoshkc

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.

08-14-2006

dwks

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:

Hexadecimal is easier to work with. Now try to write binary of 0xFF7028A4 :)

08-14-2006

Richie T

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 0-15, 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.

08-14-2006

Overlord

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.

08-14-2006

Wraithan

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.