Author Topic: I wish to learn binary and hexidecimal. Can anyone teach me please?  (Read 2854 times)

0 Members and 1 Guest are viewing this topic.

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile


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.  :-\
Challenge Trophies Won:

Offline ahayweh

  • ZX 81
  • *
  • Posts: 4
  • Karma: 1
    • View Profile
http://en.wikipedia.org/wiki/Hexadecimal

There will always be valid reasons to how this stuff. Mainly stuff like bitmasks,compression algos...

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
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
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile

Thanks, I'm beginning to grasp the concept. I think it will be mostly a matter of practice now.
Challenge Trophies Won: