Velocity Reviews > Bitwise Operator

# Bitwise Operator

somenath
Guest
Posts: n/a

 10-05-2007
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/~seande...rationCounting

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?
Regards,
Somenath

Martin Wells
Guest
Posts: n/a

 10-05-2007
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