# Assignment to Integral Types | C++ Primer Problem

Page 1 of 1

## 5 Replies - 712 Views - Last Post: 18 July 2011 - 02:36 PMRate Topic: 1 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=239930&amp;s=dd97f14f845bce83a4d789748fa816dd&md5check=' + ipb.vars['secure_hash'], cur_rating: 5, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 309
• Joined: 30-May 11

# Assignment to Integral Types | C++ Primer Problem

Posted 17 July 2011 - 10:06 PM

While studying this book, I came upon the following text:

Quote

In an unsigned type, all the bits represent the value. If a type is defined for a particular machine to use 8 bits, then the unsigned version of this type could hold the values 0 through 255.

The C++ standard does not define how signed types are represented at the bit level. Instead, each compiler is free to decide how it will represent signed types. These representations can affect the range of values that a signed type can hold. We are guaranteed that an 8-bit signed type will hold at least the values from 127 through 127; many implementations allow values from 128 through 127.

Under the most common strategy for representing signed integral types, we can view one of the bits as a sign bit. Whenever the sign bit is 1, the value is negative; when it is 0, the value is either 0 or a positive number. An 8-bit integral signed type represented using a sign-bit can hold values from 128 through 127.

I didn't understand this piece well.....maybe because I lack knowledge about a computers basic mechanism to store data. I Goggled too, but didn't get much help.....please explain this to me......

Thanks .

Is This A Good Question/Topic? 0

## Replies To: Assignment to Integral Types | C++ Primer Problem

### #2 jimblumberg

Reputation: 5864
• Posts: 17,837
• Joined: 25-December 09

## Re: Assignment to Integral Types | C++ Primer Problem

Posted 17 July 2011 - 10:32 PM

What this is saying is that the C++ standard grantees that a signed char will hold a number in the range of 127 to 127. But since this only adds up to 254 most implementations use 128 (10000000b) to 127 (1111111b) which add up to 255. And since the standard says the a signed char must hold at least 127 to 127 this will still conform to the standard.

Jim

### #3 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 309
• Joined: 30-May 11

## Re: Assignment to Integral Types | C++ Primer Problem

Posted 17 July 2011 - 10:44 PM

Thanks !

Quote

But since this only adds up to 254 most implementations use 128 (10000000b) to 127 (1111111b) which add up to 255

Will you please elaborate this statement a bit?

### #4 jimblumberg

Reputation: 5864
• Posts: 17,837
• Joined: 25-December 09

## Re: Assignment to Integral Types | C++ Primer Problem

Posted 17 July 2011 - 10:47 PM

Quote

Will you please elaborate this statement a bit?

Quote

In an unsigned type, all the bits represent the value. If a type is defined for a particular machine to use 8 bits, then the unsigned version of this type could hold the values 0 through 255.

Jim

### #5 Jeet.in

• D.I.C Regular

Reputation: 12
• Posts: 309
• Joined: 30-May 11

## Re: Assignment to Integral Types | C++ Primer Problem

Posted 17 July 2011 - 10:54 PM

Got it ! So 10000000b is a bit representation !

### #6 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Assignment to Integral Types | C++ Primer Problem

Posted 18 July 2011 - 02:36 PM

most computers use what is called the "two's complement" to represent negative integral values (integers/chars/long/short etc.)

In binary the 2's complement of 1 is -1 and is found by taking the complement (~) and then adding 1.

So looking at the char type. the value 1 is 0000 0001 lets take the 2's complement:

0000 0001 : 1
1111 1110 : ~1
1111 1111 : ~1 + 1

So -1 == 1111 1111 for the char type.

it is important to note that the 2's complement is dependent upon our "word size" or more specifically the size of our storage. so a char is 8 bits:

char (8 bits): -1 = 1111 1111
short (16 bits): -1 = 1111 1111 1111 1111
int (32 bits): -1 = 1111 1111 1111 1111 1111 1111 1111 1111
long long (64 bits): -1 = 111.... etc. 64bits all set to 1.

Note that for a short (16 bits) 1111 1111 is:

0000 0000 1111 1111 = 255 and is not -1

This is because the 16bit complement of 1 is:
1111 1111 1111 1110 add 1 -> 1111 1111 1111 1111 = -1 (16 bits)

So to re-cap here if we have -1 and we want to find -(-1) = +1 we do:

1111 1111 : -1
0000 0000 : ~(-1)
0000 0001 : ~(-1) + 1

So the two's complement of 1 is -1 and the two's complement of -1 is 1.

Not the neat thing about the two's complement is that it simplifies arithmetic.

take 2 - 5 this is 2 + (-5)
0000 0101 : 5
1111 1010 : ~5 (complement)
1111 1011 : -5 (2's complement)

now we can just add using standard addition (i.e. no need to worry about sign or anything special)

0000 0010 : 2
1111 1011 : -5
1111 1101 : (2 - 5) = 253 which is a negative value -X and to find X we can just take the 2's complement:

0000 0011 : (2's complement of 253) is 3 SO:

2 - 5 = -3

So why not just use the complement? Why this odd 2's complement.

Well first if you use the complement then -0 is 1111 1111, and since there is no such thing as -0 you have to more or less ignore it since mathematically 0 == -0 so 0000 0000 would equal 1111 1111. So there is a "repeated value" and that means we can only represent the values from -127 to 127 which is only 255 different values where as 0 - 255 is 256 different values.. seems a little wasteful.

But with the twos complement:
0000 0000 : 0
1111 1111 : ~0
0000 0000 : ~0 + 1 = 0

So the two's complement gives us: -0 = 0, and allows us to simplify arithmetic. It does have this little oddity that there is a -128 but no +128 but it DOES give all possible states of our word a unique value (i.e. for a byte you have 265 different states and each represents a unique value in the range -128 to 127)

Now it turns out that the two's complement is not generally a "feature" of the compiler, usually it is a product of the processor that code is to run upon. So for example Intel chips are generally described as: "little endian two's complement machines". This means that compilers generating code for x86 platforms will use the two's complement.

However not all machines in existence are two's complement machines. So C/C++ leaves the window open for other schemes.