Binary Numbers and Base 2 Calculation for Longs

This is a discussion on Binary Numbers and Base 2 Calculation for Longs within the Tech Board forums, part of the Community Boards category; I was recently looking into this and found it very interesting how simple it was to convert binary into a ...

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062

    Binary Numbers and Base 2 Calculation for Longs

    I was recently looking into this and found it very interesting how simple it was to convert binary into a number with base 2. I understand the highest number a bite can hold is 256 and I'm assuming whenintegers hold a maximum value of 1024 it would be four bites (Each of them being added together).

    Longs hold much higher numbers though, so it got me wondering, how big are longs generally? What's the highest value they can hold and how many bites are they usually?
    Sent from my iPadŽ

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Your thinking is out. A long is typically 4 bytes, but a 32bit value is more then four 8bit values.

    Consider, a 1bit value can hold 0 or 1, a 2bit value can hold either 00, 01, 10 or 11, (i.e. it can hold, 0, 1, 2 or 3). Continuing, you will find a bit field can hold (2 raised to the power of the number of bits)-1.

    A 32bit field can thus hold (2^32)-1 that is 0 - 4294967295, assuming it is unsigned.

    >>> highest number a bite can hold is 256

    This is also incorrect, an unsigned 8bit byte can hold 0 - 255, it can not hold 256.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Thank you for the info. All of this is still new to me, but you helped explain it alot better. Thank you.

    My only last question is, if this thinking is correct.

    unsigned ints hold 0 - 255... that is, all the combinations of 8 bits.
    signed ints hold -127 - 127(or something close to that)... that is the total amount in 7 bits and reserving the last bit to flag the bite as either positive or negitive.

    Correct?
    Sent from my iPadŽ

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, an unsigned char can hold -128 to 127.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235

    Clarification

    An unsigned byte can hold values in the range [0 .. 255].
    A signed byte can hold values in the range [-128 .. 127].
    An unsigned short integer (2 bytes, 16 bits) can hold values in the range [0 .. 65535].
    A signed short integer can hold values in the range [-32768 .. 32767].

    The architecture and programming language determine what the size of the basic integer type is. In modern systems it is usually 32 bits (4 bytes), but on some (mostly older) machines, it is 16 bits (2 bytes).
    Insert obnoxious but pithy remark here

  6. #6
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    I was recently looking into this and found it very interesting how simple it was to convert binary into a number with base 2.
    What exactly is the difference between binary and base 2?

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Nothing. Binary is base two, I was just refering to taking an byte in binary and using the calculation to convert it to a base 10 number. Looking back, I can see that statement was poorly worded.

    Code:
    10011101 =
    
    1 x 2^7 + 0 x 2^6 + 0 x 2^5 + 1 x 2^4 + 1 x 2^3 + 1 x 2^2 + 0 x 2^1 + 1 x 2^0 =
      128        0         0         16        8         4         0         1    =
      
    157
    Sent from my iPadŽ

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    signed ints hold -127 - 127(or something close to that)... that is the total amount in 7 bits and reserving the last bit to flag the bite as either positive or negitive.
    100 = 01100100b
    -100 != 11100100b

    -100 = 10011100b

    Look up 2's compliment

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    Oh thank you, that's exactly what I was looking for.

    Good man, Thantos, good man. If I could PM a drink to you, I'd certainly buy you one. Since, I can't I guess I'll just drink doubley tonight.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary to Base 10 decimal conversion
    By JFonseka in forum C Programming
    Replies: 13
    Last Post: 11-20-2007, 03:14 PM
  2. Binary representation of numbers...
    By roc in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2003, 07:42 PM
  3. adding base n numbers
    By doogle in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2002, 10:23 AM
  4. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 03:31 AM
  5. converting numbers from one base to another
    By partnole in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2001, 12:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21