Bitwise Operator

Discussion in 'C Programming' started by somenath, Oct 5, 2007.

  1. somenath

    somenath Guest

    Hello All,

    I was trying to learn bitwise operator in C .In that process I cam
    across one resource as mentioned bellow
    http://graphics.stanford.edu/~seander/bithacks.html#OperationCounting

    I have one question regarding the section

    "Compute the minimum (min) or maximum (max) of two integers without
    branching"
    In this section author is indicating "if x < y, then -(x < y) will be
    all ones" .

    My understanding is if negative number is represented in two's
    complement method then the above statement is true not for other
    representation .

    And C standard does not restrict that negative number should be
    represented in 2's complement method.

    Is my understanding correct?
    Please provide some inputs.
    Regards,
    Somenath
     
    somenath, Oct 5, 2007
    #1
    1. Advertising

  2. somenath

    Martin Wells Guest

    somenath:

    > I was trying to learn bitwise operator in C .In that process I cam
    > across one resource as mentioned bellowhttp://graphics.stanford.edu/~seander/bithacks.html#OperationCounting



    WARNING: The site assumes in about 50 thousand places that sizeof(int)
    yields the amount of value bits... IT DOESN'T.

    I've taken a look at the site and it looks quite strange. For
    instance, if I want to know the sign of a value, I'll just do:

    bool is_neg = value < 0;

    As you can see, no need for a load of crap.>


    > In this section author is indicating "if x < y, then -(x < y) will be
    > all ones" .



    Non-portable assumption that every system is two's.


    > My understanding is if negative number is represented in two's
    > complement method then the above statement is true not for other
    > representation .



    Correct.


    > And C standard does not restrict that negative number should be
    > represented in 2's complement method.



    Again correct.


    > Is my understanding correct?



    Yup.

    Martin
     
    Martin Wells, Oct 5, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. littlehobo

    Bitwise operator question...

    littlehobo, Sep 30, 2003, in forum: C++
    Replies:
    10
    Views:
    6,083
    Ron Natalie
    Oct 1, 2003
  2. Peter Shaggy Haywood

    Re: bitwise operator on a struct

    Peter Shaggy Haywood, Jun 25, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,607
    Peter Shaggy Haywood
    Jun 25, 2003
  3. Peter Shaggy Haywood

    Re: bitwise operator on a struct

    Peter Shaggy Haywood, Jun 25, 2003, in forum: C Programming
    Replies:
    0
    Views:
    832
    Peter Shaggy Haywood
    Jun 25, 2003
  4. Manish_Ganvir
    Replies:
    13
    Views:
    1,630
    Keith Thompson
    Feb 14, 2005
  5. Ioannis Vranos
    Replies:
    8
    Views:
    356
    James Kanze
    Nov 14, 2008
Loading...

Share This Page