1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

help with binary numbers

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

  1. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.38/day)
    Thanks Received:
    177
    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.:confused:
     
  2. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,998 (3.66/day)
    Thanks Received:
    14,151
    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)
     
    10 Year Member at TPU
  3. razaron

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.38/day)
    Thanks Received:
    177
    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 pyro

    crazy pyro New Member

    Joined:
    Jun 28, 2008
    Messages:
    1,662 (0.58/day)
    Thanks Received:
    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.08/day)
    Thanks Received:
    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

    IggSter

    Joined:
    Aug 24, 2007
    Messages:
    443 (0.14/day)
    Thanks Received:
    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

    razaron

    Joined:
    Apr 26, 2008
    Messages:
    1,107 (0.38/day)
    Thanks Received:
    177
    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
     
  8. lemonadesoda

    lemonadesoda

    Joined:
    Aug 30, 2006
    Messages:
    6,355 (1.80/day)
    Thanks Received:
    982
    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".
     
  9. lemonadesoda

    lemonadesoda

    Joined:
    Aug 30, 2006
    Messages:
    6,355 (1.80/day)
    Thanks Received:
    982
    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.08/day)
    Thanks Received:
    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.
     
  11. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,998 (3.66/day)
    Thanks Received:
    14,151
    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
     
    10 Year Member at TPU
  12. lemonadesoda

    lemonadesoda

    Joined:
    Aug 30, 2006
    Messages:
    6,355 (1.80/day)
    Thanks Received:
    982
    Please use your "formula" to convert 16. ;)
     
  13. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,998 (3.66/day)
    Thanks Received:
    14,151
    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
    lemonadesoda says thanks.
    10 Year Member at TPU
  14. IggSter

    IggSter

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

    10000....fixed your typo :toast:
     
  15. zaqwsx

    zaqwsx

    Joined:
    Jan 5, 2007
    Messages:
    1,144 (0.34/day)
    Thanks Received:
    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.
     

    Attached Files:

  16. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.12/day)
    Thanks Received:
    5,632
    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.
     
  17. lemonadesoda

    lemonadesoda

    Joined:
    Aug 30, 2006
    Messages:
    6,355 (1.80/day)
    Thanks Received:
    982
    ^^ Agree, There are only 10 types of people. Those that know binary, and those that dont.

    (OMG thats an old one...)
     
  18. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,998 (3.66/day)
    Thanks Received:
    14,151
    yes but thats a lot more complicated than my simple division method
     
    10 Year Member at TPU
  19. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,546 (2.42/day)
    Thanks Received:
    1,384
    Ehm, that's wrong. There are three types of people. People that can count and people that can't count.
     
    10 Year Member at TPU
  20. lemonadesoda

    lemonadesoda

    Joined:
    Aug 30, 2006
    Messages:
    6,355 (1.80/day)
    Thanks Received:
    982
    ROFL. OK

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

    zaqwsx

    Joined:
    Jan 5, 2007
    Messages:
    1,144 (0.34/day)
    Thanks Received:
    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. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.12/day)
    Thanks Received:
    5,632
    Location:
    Cheeseland (Wisconsin, USA)
    Hexidecimal FTW !!

    11011110101011011011111011101111 = DEADBEEF

    MMmmm... I smell steaks cooking !!!
     
  23. Oliver_FF

    Oliver_FF New Member

    Joined:
    Oct 15, 2006
    Messages:
    546 (0.16/day)
    Thanks Received:
    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)
    0011 0100(add the 1)
    add that up, 32+16+4 = 52; then stick the minus sign in front of it...

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

    [end-rant]
     
  24. lemonadesoda

    lemonadesoda

    Joined:
    Aug 30, 2006
    Messages:
    6,355 (1.80/day)
    Thanks Received:
    982
    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.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)