How to convert unsigned numbers from signed to unsigned(binary and hexadecimal)?

25,256

Your terminology is a bit off. You want to flip the bits (which you did), not "shift" them (which is something you'll learn about later.

What makes you think you did anything wrong? The negative of a number x is another number y such that x + y = 0. Let's look at your two numbers and add them:

  1001 1001
+ 0110 0111
-----------
  1111 1112
          ^ oops, 2 should be 10, so record 0 and carry the 1

  1111 1110
+        10 
-----------
  1111 1120
         ^ oops, 2 should be 10, so record 0 and carry the 1

Can you see where this will end up once you continue to carry the 1?

The lesson to learn here is that in order to interpret a number, you need to know whether it is intended to be a signed number or not. If it is intended to be a signed number and the sign bit is set, then in order to convert it to human output you should first negate the number, so that it is positive, and output a negative sign and the positive number.

Share:
25,256
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    So i'm trying to learn assembly language from Randall Hyde's book : The art of assembly language, and i've finished learning the first chapter and now i'm trying to do the exercises. And i have the following question : how can i convert a binary value (positive or negative) into a hexadecimal value of the opposite sign.And yes i know how to represent numbers from 0 to 15 in binary and also that 10 is f and so on... The problem that i have is the following ..i am given this number : 1001 1001 and i have to convert it in the opposite value.So far i have used the two's complement and have obtained this :

    1001 1001 ----->shifting bits -----> 0110 0110
                    add 1         -----> 0110 0111
    

    And i should've obtained the opposite of the first number.Instead,when i'm calculating the values of the results i get this:

    1001 1001 = 2^0+2^3+2^4+2^7 = 1+8+16+128 = 153 (which in my opinion is fine...)
    

    and after converting the number i get this:

    0110 0111= 2^0+2^1+2^2+2^5+2^6 = 1+2+4+32+64 = -103
    

    What am i doing wrong ?