Just out of interest
I have been programming for ages and ages, but there are some very fundamental things i still don''t know. So please don''t kill me if i say something that offends you as programmers.
Ok here is the question: a char is a byte right? (ie 8 bits). and a char can take a value between -256 and 256 right? so where is the sign of the number stored? is there an extra bit somewhere that indicates the sign? did anything i just said make sense?
Thanks
In C, a char is generally a byte (8 bits).
It''s range is from -128 to 127 (0 to 255 if unsigned).
The sign bit is held in bit 7 (2''s complement, so -1=0xff, -2=0xfe etc..).
It''s range is from -128 to 127 (0 to 255 if unsigned).
The sign bit is held in bit 7 (2''s complement, so -1=0xff, -2=0xfe etc..).
Gee Brain, what we gonna do tonight?
Just a little more info about two's complement. It is a form of binary representation used to represent signed numbers in a computer. The binary you're probably used to is called unsigned since there's really no place to put a sign bit. In two's complement, the high bit represents the sign. A 0 in the high bit means a positive number, and a 1 means negative. Positive numbers and zero are represented the same as they would be in unsigned notation, and a negative number is represented by taking the positive number, inverting the bits, and adding 1.
For instance, the number 98 is 01100010 in unsigned notation (and hence also in two's complement). If you want to form -98 in two's complement, you first invert the bits to get 10011101, then add 1 to get 10011110. The same process can be used to build a positive number from a negative one. For example, starting from -98, invert the bits (01100001) and add one (01100010) to get 98 back again.
-128 in two's complement is 10000000. Note that if you perform the two's complement operation on this:
Invert: 01111111
Add 1: 10000000
you get the same thing back. So you can't represent +128 in eight-bit two's complement, hence the range on a char is -128 to 127. Two's complement is also used to represent signed integers of other sizes, like 32-bit, not just chars. In general, the range of an n-bit integer in two's complement is -(2n-1) to 2n-1-1.
Just something good to know if you're interested in that kind of stuff.
-Ironblayde
Aeon Software
Down with Tiberia!![](http://www.aeon-software.com/misc/notiberia.gif)
"All your women are belong to me." - Nekrophidius
Edited by - Ironblayde on March 13, 2001 9:44:48 AM
For instance, the number 98 is 01100010 in unsigned notation (and hence also in two's complement). If you want to form -98 in two's complement, you first invert the bits to get 10011101, then add 1 to get 10011110. The same process can be used to build a positive number from a negative one. For example, starting from -98, invert the bits (01100001) and add one (01100010) to get 98 back again.
-128 in two's complement is 10000000. Note that if you perform the two's complement operation on this:
Invert: 01111111
Add 1: 10000000
you get the same thing back. So you can't represent +128 in eight-bit two's complement, hence the range on a char is -128 to 127. Two's complement is also used to represent signed integers of other sizes, like 32-bit, not just chars. In general, the range of an n-bit integer in two's complement is -(2n-1) to 2n-1-1.
Just something good to know if you're interested in that kind of stuff.
![](smile.gif)
-Ironblayde
Aeon Software
![](http://www.aeon-software.com/misc/notiberia.gif)
![](http://www.aeon-software.com/misc/notiberia.gif)
"All your women are belong to me." - Nekrophidius
Edited by - Ironblayde on March 13, 2001 9:44:48 AM
"Your superior intellect is no match for our puny weapons!"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement