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

  • Thread starter Thread starter Superposed_Cat
  • Start date Start date
  • Tags Tags
    cpu Work
Click For Summary

Discussion Overview

The discussion revolves around how a CPU determines the greater than (>) relationship between unsigned integers. Participants explore the mechanisms involved in comparison operations, particularly focusing on the differences between signed and unsigned integer comparisons.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant notes that while signed integers can be compared by subtracting and checking the sign bit, unsigned integers present a challenge due to their wrapping behavior when the result is negative.
  • Another participant suggests that the comparison can be performed by using an additional zero bit on the left side of the operands, effectively using a 65-bit register for the operation, although they clarify that in practice, this is handled within the existing 64-bit architecture by checking for a carry out.
  • A later reply elaborates on the role of the Arithmetic Logic Unit (ALU) in performing the subtraction and how various bits in the Program Status Word (PSW) are set based on the result, including the negative (N), zero (Z), and carry (C) bits.
  • One participant provides a code example to illustrate how the CPU would execute a comparison operation, emphasizing that for unsigned comparisons, the carry bit is relevant rather than the negative bit.

Areas of Agreement / Disagreement

Participants generally agree on the basic mechanism of using the carry bit for unsigned comparisons, but there are nuances in the explanations and methods proposed, indicating that multiple views on the implementation details remain.

Contextual Notes

Some assumptions about the architecture and behavior of the ALU and the Program Status Word may not be fully explored, and the discussion does not resolve the specifics of how different CPU architectures might implement these operations.

Superposed_Cat
Messages
388
Reaction score
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
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   Reactions: Superposed_Cat
Thank you so much! Can't believe I didn't think of that *facepalm*
 
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   Reactions: Superposed_Cat
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
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
6K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
20
Views
3K
Replies
10
Views
4K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
13K