Results 1 to 5 of 5
Thread: Number conversion question
- 02-20-2013, 05:36 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 63
- Rep Power
- 0
- 02-20-2013, 08:49 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 02-20-2013, 04:15 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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.
JimLast edited by jim829; 02-20-2013 at 04:23 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-21-2013, 07:15 AM #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 "
- 02-21-2013, 05:08 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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()); } }
JimLast edited by jim829; 02-21-2013 at 06:44 PM. Reason: corrected typo
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Help With Number Conversion
By jnomani in forum New To JavaReplies: 4Last Post: 06-06-2012, 03:47 AM -
Easy Question! Print out file, count number of lines, number of spaces w/ Scanner
By LogicalOutlier in forum New To JavaReplies: 8Last Post: 01-21-2012, 02:14 AM -
Dice help. posting the number of times a number is rolled.
By cookiejarvus in forum New To JavaReplies: 13Last Post: 12-05-2011, 12:08 AM -
Any Other Methods for Number to Word Conversion?
By arnelcolar in forum New To JavaReplies: 14Last Post: 04-14-2011, 05:45 PM -
Character conversion question
By ge2000 in forum New To JavaReplies: 5Last Post: 10-30-2010, 11:50 PM
Bookmarks