Results 1 to 5 of 5
  1. #1
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default Number conversion question

    The question:

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


    Answer is simply 180 right??

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default Re: Number conversion question

    Quote Originally Posted by abi View Post
    The question:

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


    Answer is simply 180 right??
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default 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 03:23 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    abi
    abi is offline Member
    Join Date
    Feb 2013
    Posts
    63
    Rep Power
    0

    Default 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. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default 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 05:44 PM. Reason: corrected typo
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Help With Number Conversion
    By jnomani in forum New To Java
    Replies: 4
    Last Post: 06-06-2012, 02:47 AM
  2. Replies: 8
    Last Post: 01-21-2012, 01:14 AM
  3. Dice help. posting the number of times a number is rolled.
    By cookiejarvus in forum New To Java
    Replies: 13
    Last Post: 12-04-2011, 11:08 PM
  4. Any Other Methods for Number to Word Conversion?
    By arnelcolar in forum New To Java
    Replies: 14
    Last Post: 04-14-2011, 04:45 PM
  5. Character conversion question
    By ge2000 in forum New To Java
    Replies: 5
    Last Post: 10-30-2010, 10:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •