Bitwise Operator

S

somenath

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
 
M

Martin Wells

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top