944,172 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12741
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 16th, 2007
0

Range of int

Expand Post »
Unsigned char is 1 byte long..
it's range is from -128 to 127
Most significant bit is sign bit to indicate whether the char has positive or negative value and the rest 7 bits are meant for storing the actual value of char... so the max +ve value of char can be 127 as expected but in the -ve side how can it be -128 ???
Similar Threads
Reputation Points: 14
Solved Threads: 1
Junior Poster in Training
himanjim is offline Offline
67 posts
since Jul 2006
Feb 16th, 2007
0

Re: Range of int

0xFF is the largest value that can be stored in one byte, for signed integers that is 0xFF/2, which is 127.5 (use a calculator if you must). Since you can put 0.5 in a byte, one side is 127 and the other is 128 so that 127+128 = 255 (0xFF).

limits.h contains the upper and lower limits of all numeric data types supported by your compiler.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 16th, 2007
0

Re: Range of int

The reason that it's -128 and 127 breaks down like this: You have 8 bits. One of them is for sign. That leaves 7 bits for the value, or 128 values. One side is 0 to 127. Since you already have 0, the other half is -1 to -128.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Feb 17th, 2007
0

Re: Range of int

Thanks for ur reply but there are actually 256 values 127 +128+one zero is also there.....and one more thing I can't understand 7 bits (1111111) can contain maximum of 127 so then how value 0f -128 is stored..
Reputation Points: 14
Solved Threads: 1
Junior Poster in Training
himanjim is offline Offline
67 posts
since Jul 2006
Feb 17th, 2007
0

Re: Range of int

Read up on how Two's Complement works and hopefully you'll see. It comes down to 0 being counted as a positive number, and 1-127+0 is a total of 128 numbers; there's an equal number of negatives, but since it doesn't include 0, you get 1-128.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Feb 17th, 2007
0

Re: Range of int

Click to Expand / Collapse  Quote originally posted by himanjim ...
Unsigned char is 1 byte long..
it's range is from -128 to 127
At least; it can be larger.

Click to Expand / Collapse  Quote originally posted by himanjim ...
Most significant bit is sign bit to indicate whether the char has positive or negative value and the rest 7 bits are meant for storing the actual value of char... so the max +ve value of char can be 127 as expected but in the -ve side how can it be -128 ???
Two's complement?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 17th, 2007
0

Re: Range of int

Click to Expand / Collapse  Quote originally posted by himanjim ...
Unsigned char is 1 byte long..
it's range is from -128 to 127
Just noticed... an unsigned char has values 0 to 255, and a signed char is -128 to 127.

That said, I seem to recall in some discussions that a char is strictly defined as a single byte (8 bits) by the standard but I'm not sure where to double check that...
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Feb 17th, 2007
0

Re: Range of int

Click to Expand / Collapse  Quote originally posted by Infarction ...
Just noticed... an unsigned char has values 0 to 255, and a signed char is -128 to 127.

That said, I seem to recall in some discussions that a char is strictly defined as a single byte (8 bits) by the standard but I'm not sure where to double check that...
In C (and derivatives) a byte is not defined as 8 bits.
Last edited by Dave Sinkula; Feb 17th, 2007 at 4:30 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 17th, 2007
0

Re: Range of int

Byte is not defined as 8 bits then what is it defined as?
Reputation Points: 14
Solved Threads: 1
Junior Poster in Training
himanjim is offline Offline
67 posts
since Jul 2006
Feb 17th, 2007
0

Re: Range of int

>Byte is not defined as 8 bits then what is it defined as?
It's defined as whatever the implementation wants. The standard only specifies that sizeof ( char ) be 1, and at least 8 bits. That happens to be the most common definition of a byte, but systems other than your PC also support C and don't have the same definition.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Failing an input
Next Thread in C++ Forum Timeline: Time check problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC