How does in assembly does assigning negative number to an unsigned int work?

后端 未结 5 1729
时光取名叫无心
时光取名叫无心 2020-12-20 10:36

I Learned About 2\'s Complement and unsigned and signed int. So I Decided to test my knowledge , as far as i know that a negative number is stored in 2\'s

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-20 11:18

    Both signed and unsigned are pieces of memory and according to operations it matters how they behave.

    It doesn't make any difference when adding or subtracting because due to 2-complement the operations are exactly the same.

    It matters when we compare two numbers: -1 is lower than 0 while 4294967295 obviously isn't.

    About conversion - for the same size it simply takes variable content and moves it to another - so 4294967295 becomes -1. For bigger size it's first signed extended and then content is moves.

    How does machine now - according the instruction we use. Machines have either different instructions for comparing signed and unsigned or they provide different flags for it (x86 has Carry for unsigned overflow and Overflow for signed overflow).

    Additionally, note that C is relaxed how the signed numbers are stored, they don't have to be 2-complements. But nowadays, all common architectures store the signed like this.

提交回复
热议问题