Matt I
Matt I

Reputation: 185

How do I add two hexadecimal values?

this is a school assignment. I've been given homework and one of the problems is to figure out the value after adding two hexadecimal values.

0x80000000 and 0xD0000000. I understand that D = 13, but I don't understand how the answer is 15, because 8 + 13 = 23? Could someone explain what I am doing wrong, and what I should do instead?

Thanks!

Upvotes: 2

Views: 3205

Answers (2)

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26652

It's easy if you think that every digit represents a quadruple, for example

0xDEADBEEF = 13*16⁷+14*16⁶+10*16⁵+13*16⁴+11*16³+14*16²+14*16¹+15*16⁰.

The above hexadecimal value needs an algorithm to translate to a format the the ALU can add, for instance binary numbers.

D is 13 in decimal because D is digit number 13 if A replaces 10 and so on (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F). The position of D is 7, so the number is 13*16⁷.

We notice that it is easier to LSB if we do this, and recognize that F is 15 in hexadecimal notation and therefore the number to the left will be 15*16⁰=15.

0xFF therefore means 15*16¹+15*16⁰=15*16+15=255 (you can check this with a calculator).

Now the algorithm is hopefully clear.

3735928559 is the decimal value of DEADBEEF because ==13*16^7+14*16^6+10*16^5+13*16^4+11*16^3+14*16^2+14*16^1+15*16^0=3735928559.

Upvotes: 2

Adam
Adam

Reputation: 846

Some times I convert the hexadecimal into binary base 2 this because I feel more confident to do arithmetic with binary base 2than hexadecimal. In order to do so you need to arrange every hexadecimal into group of 4 bits binary number.

 hex   0x8 + 0xD

Convert to binary

 binary  1000 + 1101 = 10101 = 21

group it again as 4 bits

 0001 0101 = 0x15

I ignored if it's signed number and didn't used two's complement.

Upvotes: 1

Related Questions