# help with binary numbers

Discussion in 'Programming & Webmastering' started by razaron, Jul 2, 2008.

1. ### razaron

Joined:
Apr 26, 2008
Messages:
1,106 (0.32/day)
178
Location:
london
i can do the alphabet in binary but the numbers after ten are confusing me
for example im not sure if, say for instance fifteen would be written as 1111 or 1010 101.

Joined:
May 14, 2004
Messages:
16,882 (3.44/day)
17,768
1111 = 8 +4 + 2 + 1

15 is especially easy since it's 16 minus 1 .. 16 is 1000 so one less is 1111 (remove leading 1 and make all other bits 1)

3. ### razaron

Joined:
Apr 26, 2008
Messages:
1,106 (0.32/day)
178
Location:
london
if 16 is 10000 then i understand 15 being 1111, but what do you mean by
1111 = 8+ 4+ 2 + 1

4. ### crazy pyroNew Member

Joined:
Jun 28, 2008
Messages:
1,662 (0.49/day)
125
Location:
Newcastle
I think that 1111 = 15 because the first one represents 8 and then the second 1 represents half the previous one, my friend was trying to explain it to me but he may have been wrong.

5. ### JoshBrunelle

Joined:
Mar 17, 2007
Messages:
258 (0.07/day)
48
Location:
Massachusetts
Keep in mind that the numbers are always block of 4. 0000 1111 would be 15, and 0001 0000 would be 16. To represent the alphabet, you would need at least 2 blocks of 4, as there are more than 16 letters (given 0000 stands for something). There are actually 52 characters (2 cases), as well as symbols, so 8 bits are enough to cover them, giving you 255 (256) different combinations. you can't just leave out 0's and still be 100% clear. So 0 would be 0000 0000, and 255 = 1111 1111

0100 0111
0110 1111
0110 1111
0110 0100
0010 0000
0100 1100
0111 0101
0110 0011
0110 1011
0010 0001

Good Luck!

Also, Wizzard was explaining the basics of binary. Each digit represent a power of 2. for example,
0010 = 2
Quite literally, this means:
0 0 1 0 = 2
0*(2^3) + 0*(2^2) + 1*(2^1) + 0*(2^0)

likewise for 1111 =15
1*(2^3) + 1*(2^2) + 1*(2^1) + 1*(2^0) = 15
8+4+2+1 = 15

Expand that to 8 places, and you have enough digits to represent the alphabet, and then some.
1000 1000
= 1*(2^7) + 1*(2^3)
= 128 + 8
= 136

Last edited: Jul 2, 2008
6. ### IggSter

Joined:
Aug 24, 2007
Messages:
443 (0.12/day)
128
Location:
BY-S36
16 8 4 2 1 = value for each binary digit
---------------------------------------------
1 0 1 0 0 = 16 + 4 = 20

0 1 1 1 1 = 8 + 4 + 2 + 1 = 15

This is also the best way to think of IP addresses, especially when using/understanding subnet masks eg:

192.168.0.1 (decimal) with the mask 255.255.0.0 (decimal)

192.168.0.1 = 11000000.10101000.00000000.00000001
255.255.0.0 = 11111111.11111111.00000000.00000000

The mask indicates that all bits = 1 are the network address and all set to zero are host addresses.

Network = 192.168.0.0
Hosts = 192.168.0.1 thru 192.168.255.254

7. ### razaron

Joined:
Apr 26, 2008
Messages:
1,106 (0.32/day)
178
Location:
london
now i get turning binary to decimels like
0010 1010=0+2+0+8+0+32+0+0
=42

0000 1111=1+2+4+8+0+0+0+0
=15

but is there no easier way to go from decimel to binary other than working out a big sum

Joined:
Aug 30, 2006
Messages:
6,373 (1.57/day)
994
15 is 1111 in binary, but is 0001 0101 in BCD (binary coded decimal). BCD takes each digit and codes it separately. As you can see, it is quicker to convert decimal to BCD and vice versa. You can also do math in BCD on many CPUs. So BCD is often used by machine code programmers when doing something "quick" without math libraries.

While math in binary is faster and more efficient (bigger numbers in less memory) the problem is translating from binary to decimal and vice versa. Its a PITA to code.

You will find ALL (or nearly all) coin-op arcade games from the 80's and 90's used BCD for their "scores" and "high scores".

Joined:
Aug 30, 2006
Messages:
6,373 (1.57/day)
994
What is the number 1111 1111? The answer is -1. If you are working with signed 8-bit binary.

10. ### JoshBrunelle

Joined:
Mar 17, 2007
Messages:
258 (0.07/day)
48
Location:
Massachusetts
Indeed using standard binary is a PITA. Had to take an entire course on machine code using an lc3 emulator. We used straight up binary and it was terrible, no idea how I passed the course.

Joined:
May 14, 2004
Messages:
16,882 (3.44/day)
17,768
15 divide by 2 = 7 remainder 1
7 divide by 2 = 3 remainder 1
3 divide by 2 = 1 remainder 1
1 divider by 2 = 0 remainder 1

-----> 1111

Joined:
Aug 30, 2006
Messages:
6,373 (1.57/day)
994

Joined:
May 14, 2004
Messages:
16,882 (3.44/day)
17,768
16 / 2 = 8 0
8 / 2 = 4 0
4 / 2 = 2 0
2 / 2 = 1 0
1 / 2 = 0 1
--> 10000

Edit: Typo fixed

Last edited: Jul 3, 2008
14. ### IggSter

Joined:
Aug 24, 2007
Messages:
443 (0.12/day)
128
Location:
BY-S36

15. ### zaqwsx

Joined:
Jan 5, 2007
Messages:
1,143 (0.29/day)
62
Location:
New York
You can convert from decimal to binary on the pc calculator. Also to convert a # to binary you could do a chart.

2^8 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
256 128 64 32 16 8 4 2 1
0 0 0 1 1 1 0 0 1

Lets say you want to convert the number 57 to binary. It all becomes addition find the numbers that will add up to 57 and put 1’s and the numbers that don’t work put 0’s. The 0’s on the left after the 1 don’t count so 57 in binary is 111001 or since its blocks of 4 0011 1001.

Look at the image because the numbers didn't come out right.

File size:
13.8 KB
Views:
183
16. ### KreijSenior Monkey ModeratorStaff Member

Joined:
Feb 6, 2007
Messages:
13,817 (3.53/day)
5,629
Location:
Cheeseland (Wisconsin, USA)
Binary representation of real world objects can be useful at times.

Let's say that you have a beer in front of you that is full (0001) and a twelve pack sitting to your left.
You drink the beer resulting in a binary zero (catastrophe!) so you shift left and grab another resulting in a binary state of 0010.
You know that once you hit 1101 you are out of beer and will have to make a run to the store. This can be prevented by a little forthought at 1010

Pffft ... and people scoff at the usefulness of binary outside of computers.

Joined:
Aug 30, 2006
Messages:
6,373 (1.57/day)
994
^^ Agree, There are only 10 types of people. Those that know binary, and those that dont.

(OMG thats an old one...)

Joined:
May 14, 2004
Messages:
16,882 (3.44/day)
17,768
yes but thats a lot more complicated than my simple division method

19. ### DanTheBanjomanSeñor Moderator

Joined:
May 20, 2004
Messages:
10,488 (2.14/day)
1,395
Ehm, that's wrong. There are three types of people. People that can count and people that can't count.

Joined:
Aug 30, 2006
Messages:
6,373 (1.57/day)
994
ROFL. OK

There are 11 types of people. Those that can count in binary and those that can't.

21. ### zaqwsx

Joined:
Jan 5, 2007
Messages:
1,143 (0.29/day)
62
Location:
New York
Ya you could use the windows calculator. Go to view then scientific then type in the number you want converted and then go from Dec to Bin. That should be easier if your doing big numbers.

22. ### KreijSenior Monkey ModeratorStaff Member

Joined:
Feb 6, 2007
Messages:
13,817 (3.53/day)
5,629
Location:
Cheeseland (Wisconsin, USA)
Hexidecimal FTW !!

MMmmm... I smell steaks cooking !!!

23. ### Oliver_FFNew Member

Joined:
Oct 15, 2006
Messages:
544 (0.14/day)
65
Speaking of Beef, i've got some serious qualms with whichever idiot coined the term "two's compliment" to read out a negative binary number...

You know, where you "invert all the bits and add 1" or whatever?

Well, instead of cacking around with that why not press the minus button on your calculator first?? idiot to the person that coined that term. At uni whenever negative numbers come up everyone starts talking about this two's compliment and I just feel like slapping them silly...

1100 1100 as a signed integer is number (-52):

Well, for the 'educated' your supposed to invert all the bits and add 1 to discover that:
0011 0011(inverted)

Much simpler:
(-128)+64+8+4 = (-52)

[end-rant]

Joined:
Aug 30, 2006
Messages:
6,373 (1.57/day)
994
Oliver, you need to think about how this is done in machine code. You have instructions on a CPU that handle binary arithmetic. You cant do "blackboard math" on a CPU. If you are interested go here:

http://6502org.wikidot.com/software-math
http://codebase64.org/doku.php?id=base:6502_6510_maths
http://forum.6502.org/viewtopic.php?p=6338&sid=5cffd0599b1bce4d5fb06420d4b27203
http://www.atarimax.com/freenet/fre.../7.TechnicalResourceCenter/showarticle.php?14

(I'm giving you links to 6502 code because that was one of the top 8-bit processors from the 70's and 80's ... and the foundation of college machine code math)

Until you see it in practice, it is conceptually difficult to know why it is done like this. You cant imagine how tough it was to program back then without compilers, IDEs, and math libraries that are standard today.

Oliver_FF says thanks.