Add Short Ints: 30064 + 30064 = -60128

  • Thread starter Thread starter Lightf
  • Start date Start date
  • Tags Tags
    Integers Short
Click For Summary

Discussion Overview

The discussion revolves around the calculation of the sum of two short integers, specifically the expression 30064 + 30064, and the interpretation of the result in the context of short integer representation in programming languages like C or C++. Participants explore how the conversion to hexadecimal and the properties of short integers lead to a negative result.

Discussion Character

  • Homework-related
  • Mathematical reasoning
  • Technical explanation

Main Points Raised

  • One participant describes their process of converting 30064 to hexadecimal (7570) and adding it to itself, resulting in eae0, but expresses confusion about obtaining a negative result.
  • Another participant clarifies that short integers in C or C++ are signed, with the highest order bit indicating negativity, and explains that adding values can lead to overflow, resulting in negative numbers.
  • A different participant proposes a calculation involving the maximum positive short int (7fff) and suggests a method to derive a negative value (-151f) from the hexadecimal result.
  • One participant mentions the binary representation of eae0 and notes that the leading bit indicates it is a negative number, suggesting a value of -27360.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correct interpretation or calculation method, with differing views on how to arrive at the negative result from the addition of the short integers.

Contextual Notes

There is an implicit assumption regarding the handling of overflow in short integer arithmetic, and the discussion does not resolve how to consistently apply the two's complement method for these calculations.

Lightf
Messages
16
Reaction score
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
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.
 
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.
 
You may want to look for "two's complement" in your textbook or on the net.
 
So eae0 is 1110101011100000 but the first 1 just means its a minus number so its -27360
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
8K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
2
Views
1K
  • · Replies 19 ·
Replies
19
Views
2K
Replies
11
Views
6K
  • · Replies 20 ·
Replies
20
Views
3K
Replies
2
Views
26K
  • · Replies 7 ·
Replies
7
Views
3K