Like Tree1Likes
  • 1 Post By megafiddle

Finding Pos/Neg Integers Unsigned Can Hold

This is a discussion on Finding Pos/Neg Integers Unsigned Can Hold within the C Programming forums, part of the General Programming Boards category; Consider a new data type, the mikesint, which can hold 9 bits. (a) What is the largest integer that an ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    4

    Finding Pos/Neg Integers Unsigned Can Hold

    Consider a new data type, the mikesint, which can hold 9 bits.
    (a) What is the largest integer that an unsigned mikesint can hold?
    (b) What is the largest positive integer that a signed mikesint can hold?
    (c) What is the largest negative integer that a signed mikesint can hold?

    --->
    Not sure how to determine this. I'm stuck. Please help!

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    Quote Originally Posted by trial123 View Post
    Consider a new data type, the mikesint, which can hold 9 bits.
    (a) What is the largest integer that an unsigned mikesint can hold?
    (b) What is the largest positive integer that a signed mikesint can hold?
    (c) What is the largest negative integer that a signed mikesint can hold?

    --->
    Not sure how to determine this. I'm stuck. Please help!
    It's quite simple..think about what number would be if all 9 bits were 1 (on)? Just count up binary, thats the largest 'unsigned' number that 'mikesint' can hold.

    Now consider that a signed type takes (at least) 1 bit to store the 'signedness' of the number and that leaves you 8 bits to store the number (again all set to 1 gives the largest number, negative and positive). There is a second piece to consider however, and that is WHICH bit to use for the signedness. If you use the 'least significant bit' or the 'rightmost' ie the 1 switch in binary. Then that is going to dramatically increase the maximum number you could store in a mikesint, as compared to if you used the 'most significant bit' or leftmost bit to store the signedness (which would be a bad idea for this reason).

    That should tell you how to figure out your answers.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    4
    It's quite simple..think about what number would be if all 9 bits were 1 (on)? Just count up binary, thats the largest 'unsigned' number that 'mikesint' can hold.
    So, that would be... 2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 + 2^7 + 2^8 + 2^9 = 1023?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You're adding ten terms there. Nine bits cant hold that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    (Posted by iMalc)
    Last edited by nonpuz; 01-25-2013 at 10:53 PM. Reason: Redundancy

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    498
    8 bits can code 256 different values - 2^8 = 256
    Unsigned value range is 0 - 255

    9 bits can code 512 different values - 2^9 = 512
    Unsigned value range is 0 - 511

    10 bits can code 1024 different values - 2^10 = 1024
    Unsigned value range is 0 - 1023

    Since one of the codes is zero, the maximum unsigned value for N bits is (2^N)-1.

    If you use one of the bits for the sign, you lose one bit for the value magnitude.
    So a signed 8 bit value has the same maximum value as an unsigned 7 bit value, etc.
    Therefore the maximum positive signed value is ( 2^(N-1) ) -1.

    For a signed value, the codes '0', through the maximum positive value, will use up 1/2 of the possible codes for N bits,
    ( one bit for sign, N-1 bits for magnitude ).
    This leaves the remaing half of possible values to code the negative values. So the maximum negative value is -( 2^(N-1) ).
    iMalc likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Validating unsigned integers with scanf()
    By Mr_Miguel in forum C Programming
    Replies: 7
    Last Post: 03-04-2008, 01:51 PM
  2. Getting integers to hold zeros
    By Wolves in forum C++ Programming
    Replies: 15
    Last Post: 05-27-2007, 06:04 AM
  3. 32-bit unsigned integers?
    By Wiretron in forum C Programming
    Replies: 7
    Last Post: 08-28-2006, 11:30 AM
  4. 64-BIT unsigned integers - Borland 5.5
    By maththeorylvr in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2006, 05:10 PM
  5. unsigned integers
    By lydiapeter in forum C++ Programming
    Replies: 6
    Last Post: 09-15-2005, 04:59 PM

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