1. Member
Join Date
Feb 2013
Posts
63
Rep Power
0

## Number conversion question

The question:

The hexadecimal number B4 is a 2's complement number in 8-bit binary. Convert it to decimal.

2. ## Re: Number conversion question

Originally Posted by abi
The question:

The hexadecimal number B4 is a 2's complement number in 8-bit binary. Convert it to decimal.

It's 11*16+4 == 180, so you're right; why not build a simple calculator that does these nasty little things for you?

kind regards,

Jos

3. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,709
Rep Power
4

## Re: Number conversion question

Well you said that B4 is an 8 bit, 2's complement number. So I would have said -76.

76 in hex is 4C. Take the 1's complement of that value in an 8 bit field and you get B3. Add 1 to get the 2's complement representation to
get B4. So B4 is -76 in an 8 bit, 2's complement representation.

If you just want to convert B4 in hex to decimal then 180 is the correct answer.

Jim
Last edited by jim829; 02-20-2013 at 04:23 PM.

4. Member
Join Date
Feb 2013
Posts
63
Rep Power
0

## Re: Number conversion question

The hexadecimal number B4 is a 2's complement number in 8-bit binary. Convert it to decimal.
That 2's compliment bit was what was throwing me off i.e. do i solve it regularly as Jos did above or is there some other way which i guess would be the way you are doing it.

Could you explain what your doing up there? What do you mean when you say "Take the 1's complement of that value in an 8 bit field "

5. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,709
Rep Power
4

## Re: Number conversion question

Negative numbers are represented in 2's complement form. 1's complement just means taking the complement of a binary number (i.e. change 1's to 0's and 0's to 1's). Two's complement means doing the same thing but then adding 1 to the result. This latter operation is how negative numbers are represented in binary.

Some processors can (or used to) work with 1's complement as the negative representation of a number. The problem is there were two values of 0. There was 0 itself. And then there was the sum of a negative number and its positive counter part which resulted in all ones.

Here is an example
5 = 00000101
-5 = 11111010 (in 1's complement)
0 = 11111111 (their sum)

Here is the example in two's complement
-5 = 11111010 (1's complement)
+ 1
-5 = 11111011 (2's complement)
5 = 00000101
0 = 00000000

Notice the conversion to 2's complement cause the addition of all the ones to carry over.
The final carry is "lost" because it exceeds the width of the type. If you would like to see
some examples of this is Java try the following:
Java Code:
```public class Binary {

public static void main(String[] args) {
int a = 5;
int b = -5;

String abits = Integer.toBinaryString(a);
//pad out with 0's on the left
abits = "00000000000000000000000000000000".substring(abits.length()) + abits;
String bbits = Integer.toBinaryString(b);
System.out.println(a + " = " + abits);
System.out.println(b + " = " + bbits);
String ahex = Integer.toHexString(a);
String bhex = Integer.toHexString(b);
//pad out with 0's on the left
ahex = "00000000".substring(ahex.length()) + ahex;
System.out.println(a + " = " + ahex.toUpperCase());
System.out.println(b + " = " + bhex.toUpperCase());

}

}```
Regards,
Jim
Last edited by jim829; 02-21-2013 at 06:44 PM. Reason: corrected typo