hackman2007 wrote:

Stupid question, but does this have anything to do with learning C++? I've sort of found out how to do it.

It is how all modern computers represent numbers internally. You need to know this.

Binary numbers and manipulating them will come in handy later down the road.

There will be times when you are coding that you need to use something called a bit-mask. Which can be used in binary or hex form. I use it quite often when I do some network programming and even html programming.

Here is an example.

Color representation on a computer is typically represented by a 3-tuple normally called RGB values. For example, a white colored pixel has the value (255,255,255) while a black color pixel has the value (0,0,0). Pure red is (255,0,0), pure green is (0,255,0) pure blue is (0,0,255).

It takes 8 bits to represent 256 numbers. In RGB color representation, a computer uses 8 bits for red, 8 bits for green, and 8 bits for blue, (and sometimes 8 bits for transparency). Since we need 24 (or sometimes 32) bits to represent a pixel, it is perfect to use an int(eger) since an integer uses 32 bits. That means you can pack a pixel value consisting of 3 colors (and possibly a transparency value) all into just 1 integer.

So for example, pure white can be represented by...

red = 11111111

green = 11111111

blue = 11111111

So in a 32 bit (big endian) system (don't worry about the big endian term for now), you could represent pure white with an int value that is equal to

a binary of:11111111111111111111111100000000 where the first 8 bits of the 32 bit int represents red, the second 8bits represent green, and so on.

That huge binary number is a pain in the ass, so Hex numbers are used.

So...hex numbers have this property where 4 bits can be represent by just 1 hex digit.

For example:

15 (decimal) = 1111 (binary) = F (hex)

Example 2:

10 (decimal) = 1010 (binary) = A (hex)

Now let's go back to the color example I used earlier...

A color of (255,255,255) packed into one integer can be represented in binary by: 11111111111111111111111100000000

We can represent that in a much easier way by "breaking it up" into smaller pieces and converting it to hex... Take 4 binary bits at a time and convert them to hex...so we get

FFFFFF00.

Each "F" represent 1111.

You can even see web pages use this hex concept...for example:

If I want to use a red background for a web element, I would specify:

bgcolor=#FF0000

Which represents 111111110000000000000000 which if you split this binary number into 8 bit groupings, you get 11111111 00000000 0000000 which represents (255,0,0) which is red.