Thread: confuse with range of Data Typpe

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    confuse with range of Data Typpe

    I understand that if the size of register is 8 bit wide then It can be store only 8 bit data

    Example int number = 12345678 // it store 8 bit data

    Example int number = 123456789 // it will give error because data is 9 bit wide


    Below are example of int Data type

    [8-bit] signed int: -127 to 127

    [8-bit] unsigned int: 0 to 255

    [8-bit] signed char: -127 to 127 or 0 to 255

    I am confuse with negative range -127 to 127 ? I think 8 bit register can store only 0 to 255 numbers and the number can positive or negative
    Last edited by abhi143; 10-25-2017 at 11:31 PM.

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    for signed decimal:
    0 decimal is 0000 0000 binary
    1 decimal is 0000 0001 binary
    2 decimal is 0000 0010 binary
    3 decimal is 0000 0011 binary

    125 decimal is 0111 1101 binary
    126 decimal is 0111 1110 binary
    127 decimal is 0111 1111 binary
    128 decimal is 1000 0000 binary

    -127 decimal is 1000 0001 binary
    -126 decimal is 1000 0010 binary
    -125 decimal is 1000 0011 binary
    -124 decimal is 1000 0100 binary

    -4 decimal is 1111 1100 binary
    -3 decimal is 1111 1101 binary
    -2 decimal is 1111 1110 binary
    -1 decimal is 1111 1111 binary

    So what you get in a sense is that binary representation of signed decimal begins at 0, continues to 128, then jumps down to -127 and counts up to -1. Whether you cover positive numbers or not, the range of an 8 bit number must be no more than 256 numbers. If you want both positive and negative 255, you need a range of 512 which means a ninth bit.
    Last edited by jack jordan; 10-26-2017 at 12:02 AM.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    ...aaaActually, "1000 0000" is -128, as per two's complement. The range is [-pow(2, n/2), pow(2, n/2)-1], where "n" is the amount of bits used.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I got that one wrong because I was using an 32 bit calculator rather than an 8 bit calculator.

    So it goes from 0 to 127, then -128 to -1.

    Fun fact:
    -128 is to 8 bits as
    -9,223,372,036,854,775,808 is to 32 bits
    Last edited by jack jordan; 10-26-2017 at 12:32 AM.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    129
    Hey I am still confuse

    confuse with range of Data Typpe-memory-jpg

    It can be store 0 to 255 address means it can store 0 to 255 variables
    it can be store 8 bit size of Data
    Last edited by abhi143; 10-26-2017 at 01:18 AM.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by abhi143 View Post
    It can be store 0 to 255 address means it can store 0 to 255 variables
    it can be store 8 bit size of Data
    Not necessarily. The Commodore 64 was an 8-bit computer but it had 64 KiB of RAM and could access even more than that, thanks to "bank switching". It effectively had 16-bit addresses of 8-bit data.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Quote Originally Posted by abhi143 View Post
    I understand that if the size of register is 8 bit wide then It can be store only 8 bit data
    CHAR_BIT is defined in header <limits.h>.

    Example int number = 12345678 // it store 8 bit data

    Example int number = 123456789 // it will give error because data is 9 bit wide


    Below are example of int Data type

    [8-bit] signed int: -127 to 127

    [8-bit] unsigned int: 0 to 255

    [8-bit] signed char: -127 to 127 or 0 to 255

    I am confuse with negative range -127 to 127 ? I think 8 bit register can store only 0 to 255 numbers and the number can positive or negative
    Bit counting starts from bit 0, bit 1, and so on. You better use unsigned values for bit-twiddling. For example see this link.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Type Range
    By Khaltazar in forum C Programming
    Replies: 5
    Last Post: 04-09-2013, 12:49 AM
  2. Range of data types in C..
    By Rehman khan in forum C Programming
    Replies: 11
    Last Post: 02-02-2012, 03:26 PM
  3. Replies: 8
    Last Post: 12-04-2010, 01:34 AM
  4. Replies: 2
    Last Post: 01-31-2008, 09:52 AM

Tags for this Thread