How do you express negative numbers in hexadecimal? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do you express negative numbers in hexadecimal?

For exmaple how do you express -2 in hexadecimal in computer languages?

28th Jun 2018, 1:26 AM
Rory Zhao
Rory Zhao - avatar
4 Answers
+ 4
Considering a 8 bit register for 8 bits of the number including the sign bit (i.e storing 7 bits for the number and 1 bit for the signed).So we do 2's complement to -2 it becomes +2 = 0000 0010 1111 1101 (1's comp) + 1 _____________ -2 = 1111 1110 (2's comp) Convert this (11111110) binary to hexadecimal and you get hexadecimal value fe. Therefore, -2 is (fe) in hexadecimal. So you can represent with - sign in programming language.And so -2 can be represented as , -0x2 8 bit register :- - 0x02 or 0xfe(hex) 16 bit register:- -0x0002 or 0xfffe(hex) 32 bit register:- -0x00000002 or 0xfffffffe(hex) So the above is data represtation for -2 in 8,16,32 bit registers and in computer prgramming. For x86 or x85 family of 8086 and 8085 micrprocessors of assembly Programming (low level language), we can directly declare -2 as databyte db like .data number db -2 .code mov ax,number mov ds,ax or mov ax,FEH mov ds,ax Hence all the ways of expressing -2.
28th Jun 2018, 4:43 AM
D-Key
D-Key - avatar
+ 5
You could make use of unary - and use -0x2. You could also do 0xfffffffe (32bit integer.)
28th Jun 2018, 2:00 AM
John Wells
John Wells - avatar
+ 4
Rory Zhao My gratitude and Thanks for marking my answer as best!!
29th Jun 2018, 7:28 AM
D-Key
D-Key - avatar
+ 3
You can just write it as -2. Hexadecimal and decimal are the same thing, it's just that hex has some more digits (ABCDEF). So -10 in dec would be -A in hex. The minus sign hasn't much to do with number bases. If you are asking how computers store negative numbers that's a bit different. They use what's called two's complement; you have to dive into binary for that.* To get from say 10 to -10 you'd flip all the bits and add 1. So if you're on a machine with 8 bit wide numbers, here's an example: 0000 1010 bin == 10 dec 1111 0110 bin == -10 dec So you could say -10 in dec is either -A in hex, which is always a correct way to say it, OR it is also F6 in hex on an 8-bit computer which uses two's complement numbers. The way you can tell whether a number is negative on a computer is by looking at the leftmost bit. 0 is positive, 1 is negative. __ * You can group exactly 4 binary digits into 1 hexadecimal one.
28th Jun 2018, 1:51 AM
Schindlabua
Schindlabua - avatar