CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 1999
    Posts
    7

    Very basic bytes

    Can somebody help me in recognizing the binary values ??

    I've read that 11111000 is equal to -8. I understand the reason is because the high order bit is 1.
    Why shud it not be interpreted as -120 ?? And if at all I need to write -120 in binary form how shud
    I go about it ??

    Thanks,
    rams


  2. #2
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Very basic bytes


    To find the binary value for negative numbers , do the following..

    for -8 :

    1. +8's binary value minus one..


    00000....1000 -1 = 00000.... 0111




    2. Take 2's (1's ? ) compliment of the result.



    11111.... 1000 = -8





    for -120 :

    1. +120's binary value minus one



    0000... 0111 1000 - 1 = 0000... 0111 0111




    2. 2's(1's?) compliment of the result.. ( 0 becomes 1 and 1 becomes 0 )



    1111.... 1000 1000 = -120






    Poochi..




  3. #3
    Join Date
    Jul 1999
    Posts
    7

    Re: Very basic bytes

    Dear Poochi,

    thanks for responding to my question.
    but i am still confused. could you please explain me a little bit on the following.

    i understood how to convert numbers to negative using 2's complement and adding 1 to the result.
    but incase somebody gives me the number say 1111 1000 and asks me what it represents i will go with
    the idea that because the high order bit is 1 its a negative number and calculating the value frm the
    remaining numbers its 120. so i will end up saying -120 which is not true. can u help me with this ??

    thanks,
    rams.


  4. #4
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: Very basic bytes


    Hi Ram,

    In 2's compliment notation , the higher order bit always represent negative value.
    If you see 1 in the higher order bit you can assume that the binary value represent
    a negative number and you can proceed with that assumption.

    > but incase somebody gives me the number say 1111 1000 and asks me what it represents i will go with
    > the idea that because the high order bit is 1 its a negative number and calculating the value frm the
    > remaining numbers its 120. so i will end up saying -120 which is not true. can u help me with this ??

    How did you get the number 120 from 1111 1000 ? you have to do the reverse engg. here..
    O.K if the higher order bit is 1 , then you can assume that the number is negative ,right ?

    Take 1's compliment of it and add 1 with the result ( shortly , we can say 2's compliment )



    1111 1000 -> 1's Compliment -> 0000 0111 + 1 -> 0000 1000 = 8




    I know it's a negative number. So , it's -8.

    Check the output of the following program



    class Binary{
    public static void main( String[] str ){
    int i = 0x80000000;
    System.out.println( i );
    }
    }





    You will get the following output..



    -2147483648





    The bit pattern is



    1000 0000 0000 0000 0000 0000 0000 0000





    Since the 2's compliment's higher order bit always represent signed value , it's a negative
    number. Take 2's compliment of it ( you will get the same bit value ) and convert it
    to decimal and put minus infront of it.

    I hope i am not confusing you..

    Poochi..


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured