Add Short Ints: 30064 + 30064 = -60128

In summary, when converting to short int and adding 30064 + 30064, the answer should be -5497 in decimal. This is achieved by converting 30064 to hex (7570), adding it to itself (eae0), and then subtracting 8000 from the result (-151f). This is because short ints are 32 bit signed numbers, and any number with a highest order bit of 1 is considered negative.
  • #1
Lightf
16
0

Homework Statement



The question is :

Converting to short int, calculate 30064 + 30064 as short integers.
Convert the answer to decimal ( the answer will be negative ).

Homework Equations



None.

The Attempt at a Solution



I converted 30064 to hex getting 7570.

I then added that to itself getting eae0.

But when i convert that back to dec I just get 60128 ? How am i suppose to get a negative number?
 
Physics news on Phys.org
  • #2
Lightf said:

Homework Statement



The question is :

Converting to short int, calculate 30064 + 30064 as short integers.
Convert the answer to decimal ( the answer will be negative ).

Homework Equations



None.

The Attempt at a Solution



I converted 30064 to hex getting 7570.

I then added that to itself getting eae0.

But when i convert that back to dec I just get 60128 ? How am i suppose to get a negative number?

Since you're talking about short integers, I'm assuming you are talking about the short int data type in C or C++. Short ints are 32 bit signed numbers, which means that the largest positive short int is 32,767, or 0x7ffff. Adding 1 to this gives you 0x8000, which is -32,768.

Any short int whose highest order bit is 1 is considered to be negative.
 
  • #3
So the answer would be :

7fff - eae0 = 6ae1

6ae1 - 8000 = -151f

which is -5497 in dec.

Is that the correct way of doing it? Thanks for the help.
 
  • #4
You may want to look for "two's complement" in your textbook or on the net.
 
  • #5
So eae0 is 1110101011100000 but the first 1 just means its a minus number so its -27360
 

FAQ: Add Short Ints: 30064 + 30064 = -60128

1. What is the result of adding two short integers with values 30064 and 30064?

The result of adding 30064 and 30064 as short integers is -60128.

2. Why is the result of adding two positive integers negative?

The result is negative because the maximum value for a short integer is 32767. When adding two numbers that exceed this limit, the result overflows and wraps around to the negative range.

3. Is there a limit to the maximum value for a short integer?

Yes, the maximum value for a short integer is 32767.

4. Can short integers represent decimal numbers?

No, short integers can only represent whole numbers within their specified range. They cannot represent decimal numbers.

5. Why are short integers used instead of other data types?

Short integers are used because they take up less space in computer memory compared to other data types, making them more efficient for storing and processing large amounts of data.

Similar threads

Replies
9
Views
7K
Replies
7
Views
2K
Replies
2
Views
3K
Replies
19
Views
2K
Replies
20
Views
2K
Replies
7
Views
2K
Back
Top