Public Group

#### Archived

This topic is now archived and is closed to further replies.

# signed and unsigned

This topic is 5943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I just have one question. I am reading a C++ book and it is telling me about signed and unsigned integers. I dont understand why you put unsigned short int instead of just int. I dont fully understand what it does so i kept reading and it keeps going back to it. If someone wants to can you explain to me what they are. Thanks When The Only Thing You Have Is A Hammer, Everything Looks Like A Nail [edited by - stevelevesque on July 15, 2002 10:59:01 AM]

##### Share on other sites
A signed variable means that it can be negative, whereas an unsigned variable can only be positive. Unsigned can hold a larger number too, since the MSB is not used to tell the sign of the number but used in the actual number. Short integers are smaller and can''t hold as large of a value as an int. Long is the opposite, it can hold a larger number than an int.

-SirKnight

##### Share on other sites
How do you know when to put those inside your code and not something else like just int.

When The Only Thing You Have Is A Hammer, Everything Looks Like A Nail

##### Share on other sites
In the case of 16 bit integers (picking a random example) an unsigned int will use the full 16 bits to create a positive number:

0000000000000000 = 0
to
1111111111111111 = 65535

whereas a signed integer reserves the left most bit to indicate whether the number is positive or negative with the next 15 bits representing the actual number:

0111111111111111 = -32767
to
1111111111111111 = 32767

Hope that helps a little!

##### Share on other sites
Umm, don''t you mean
2''s complement binary 0111111111111111 = +32767
2''s complement binary 1111111111111111 = -32768
???
The MSB represents -32768 = (-(2^15)) in 16-bits (signed).

##### Share on other sites
Umm, don''t you mean
2''s complement binary 0111111111111111 = +32767
2''s complement binary 1111111111111111 = -1
???
The MSB represents -32768 = (-(2^15)) in 16-bits (signed).

-- might have posted this twice, with an incorrect version earlier on. Sorry.

##### Share on other sites
yup.. that''s what I should''ve had.. my mind was elsewhere I think!

##### Share on other sites
quote:
Original post by stevelevesque
How do you know when to put those inside your code and not something else like just int.

It depends on the needs of the application and the operating system. If you don''t expect a value outside than the range of a short, then a short can be used. However, on a 32 bit system, a 32 bit data type will work more efficiently than a 16 bit datatype. So even if you don''t expect the value to fall outside of the range of a short, using an int or a long can improve performance. For example:

for( int i = 0; i < 100; i++) {
// do stuff
}

So when would you want to use a short? If an api call requires it. If a file format requires it. If the system is 16 bit and so forth.

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 12
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632991
• Total Posts
3009749
• ### Who's Online (See full list)

There are no registered users currently online

×