Dark Bit Factory & Gravity
PROGRAMMING => General coding questions => Topic started by: Pixel_Outlaw on October 09, 2007
-
Hi recently I've got the urge to stop pussyfooting around the issue and learn binary and hexidecimal number systems for programming use. It seems rare that either are used but there are just some situations that demand it. Perhaps I'm even missing valuable opertunities to write better code because of my ignorance. i'm tired of bein an uninformed sub par programmer I want to know what they would teach a student in a trade school. I've taught myself eveything I've learned over about 5 years and as a result I feel that I lack some very basic concepts. :-\
-
http://en.wikipedia.org/wiki/Hexadecimal
There will always be valid reasons to how this stuff. Mainly stuff like bitmasks,compression algos...
-
Think of it like hundreds, tens and units that you use naturally with base 10. The columns there are 1s, 10s, 100s, 1000s (powers of 10), .... In hex, they are 1s, 16s, 16x16s (256s), 16x16x16s (4096s) (powers of 16), ...The numbers between 9 and 15 are represented by letters.
10=A
11=B
12=C
13=D
14=E
15=F
So when you see a number like 7fe, then you think
7*16*16+f*16+e
=7*256+f*16+e
=1792*15*16+14
=1792+240+14
=2046
Binary is the same, except the columns are powers of 2, ie 1,2,4,8,16,32,64...
So the number
1011
=1*8 + 0*4 + 1*2 + 1*1
=11
There are real benefits to using the different representations. Often computers work in bits, and you might want to isolate one of those bits from a byte. C unfortunately doesn't support binary as a number format.
When we work with colours we often use a 32 bit integer for each pixel. That integer contains the alpha, red, green, blue for each pixel.
Using hex we can isolate each part, which is 0-255 decimal by doing
blue = colour & ff
green = colour & ff00
red = colour & ff0000
I had a great tutorial for this at the yabasic forums. Can anyone find it?
Please post again if there's confusion - this isn't a great post...
Jim
-
Thanks, I'm beginning to grasp the concept. I think it will be mostly a matter of practice now.