How does the cpu work out greater than (>) in unsigned ints?

  • Thread starter Superposed_Cat
  • Start date
  • Tags
    cpu Work
In summary: Nuqatory.In summary, when performing operations on unsigned integers, the ALU sets different bits in the Program Status Word to indicate the result. The carry bit is used for unsigned comparisons and the CPU uses these bits to determine the outcome of a comparison and execute the appropriate code.
  • #1
Superposed_Cat
388
5
Hey all, I understand how the cpu does most things but I can't figure out how it would work out greater than or less than in unsigned integers, in signed you could simply subtract them and check the highest bit for sign (I assume) but unsigned ints will loop back to the greatest value if A-B where B>A, any help apreciated.
 
Engineering news on Phys.org
  • #2
Superposed_Cat said:
Hey all, I understand how the cpu does most things but I can't figure out how it would work out greater than or less than in unsigned integers, in signed you could simply subtract them and check the highest bit for sign (I assume) but unsigned ints will loop back to the greatest value if A-B where B>A, any help apreciated.

You just do the subtraction with one extra zero bit on the left side of the operands. If you're comparing 64 bit integers, the operation is done in a 65-bit register. (In practice, the CPU designer doesn't mess with constructing actual 65-bit registers and data paths, we just do the subtraction in 64 bits and check for a carry out of the leftmost bit).
 
Last edited:
  • Like
Likes Superposed_Cat
  • #3
Thank you so much! Can't believe I didn't think of that *facepalm*
 
  • #4
To Expand a bit on what Nuqatory said its all in the operation of the ALU
When the ALU performs an operation, in this case a subtraction, a number of bits may get set in the Program Status Word.
For example
N: Result was negative
Z: Result was Zero
C: Result had a carry out

So if you did a piece of code
Code:
if(a>b) then
   do something
else
  do something else

the CPU would undertake the following actions
Code:
Load a into Register1
Load b into Register2 
Register1-Register2 
Branch If N bit set to LABEL1
Do something 
Branch to LABEL 2
LABEL1: 
Do something else
LABEL 2:

hope that makes sense :)
 
  • Like
Likes Superposed_Cat
  • #5
cpscdave said:
the CPU would undertake the following actions
Code:
Load a into Register1
Load b into Register2
Register1-Register2
Branch If N bit set to LABEL1
Do something
Branch to LABEL 2
LABEL1:
Do something else
LABEL 2:

hope that makes sense :)

Of course the carry bit and not the N bit would be used for an unsigned comparision
 

1. How does the CPU compare unsigned integers using the greater than (>) operator?

The CPU uses a set of logical instructions and circuits to compare two unsigned integers using the greater than (>) operator. This involves comparing the bits of each integer and determining which one is larger.

2. What is the process involved when the CPU encounters the greater than (>) operator in unsigned integer comparisons?

When the CPU encounters the greater than (>) operator in unsigned integer comparisons, it first checks if the most significant bit (MSB) of each integer is 1. If one integer has a 1 in its MSB and the other does not, the integer with the 1 in its MSB is automatically larger. If both integers have a 1 in their MSB, the rest of the bits are compared to determine the larger integer.

3. How does the CPU handle unsigned integer comparisons with the greater than (>) operator when the integers have the same value?

If the two unsigned integers being compared have the same value, the CPU will compare their bits and find that they are equal. In this case, the greater than (>) operator will return a false value as the two integers are not truly greater than one another.

4. Is there a difference in how the CPU handles greater than (>) comparisons with signed versus unsigned integers?

Yes, there is a difference in how the CPU handles greater than (>) comparisons with signed and unsigned integers. Signed integers use the most significant bit to indicate whether the integer is positive or negative, while unsigned integers do not have this distinction. Therefore, the CPU uses different logic and instructions for comparing signed and unsigned integers.

5. Can the greater than (>) operator be used to compare other data types besides unsigned integers?

Yes, the greater than (>) operator can be used to compare other data types besides unsigned integers. It can be used to compare signed integers, floating-point numbers, characters, and other data types. However, the comparison process may differ depending on the data type being compared.

Similar threads

Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Electrical Engineering
Replies
33
Views
596
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Electrical Engineering
Replies
11
Views
2K
  • Introductory Physics Homework Help
Replies
2
Views
483
  • Electrical Engineering
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
Replies
10
Views
3K
Back
Top