what does it do?

M

Michael Mair

puzzlecracker said:
what does it do?
#define BLACKBOX(x) ((x)&((x)-1))

For unsigned integers x and signed integers x in 2s complement
with x>(integer type's minimum value), this clears the least
significant set bit.
It also works for 1s complement signed integers and
x!=0 && x!=1 && x>(integer type's minimum value)

-Michael
 
L

Luke Wu

puzzlecracker said:
what does it do?
#define BLACKBOX(x) ((x)&((x)-1))

Clears the least significant set bit in x. This works on all unsigned
numbers and all signed numbers (except "sign-magnitude" negative even
numbers).
 
M

Michael Mair

Luke said:
Clears the least significant set bit in x. This works on all unsigned
numbers and all signed numbers (except "sign-magnitude" negative even
numbers).

.... and except for x==T_MIN where T is the respective integer type;
in the case of 1s complement you cannot rely on 0 not switching
its representation, so x==0 and x==1 are not guaranteed to work
either.


Cheers
Michael
 
P

Peter Nilsson

What do you think it does?

If you want to 'test' clc, at least try to be original.

Michael said:
For unsigned integers x and signed integers x in 2s complement
with x>(integer type's minimum value), this clears the least
significant set bit.
It also works for 1s complement signed integers and
x!=0 && x!=1 && x>(integer type's minimum value)

No, consider the following (high bit is sign-bit):

1c or 2c(C99) sm
x : 10000000 10000000 10000000 01111111
x - 1: 10000000 01111111 10000000 10000000
& : 10000000 00000000 10000000 00000000

The result could be a trap representation.

Note also that C89 potentially allows negative representations
beyond 2c, 1c and sm.
 
R

Raymond Martineau

Clears the least significant set bit in x. This works on all unsigned
numbers and all signed numbers (except "sign-magnitude" negative even
numbers).

Inputting the integer '8' causes the "function" to return zero. Not
suprizing, since the original poster is a known troll.
 
B

Ben Pfaff

Inputting the integer '8' causes the "function" to return zero. Not
suprizing, since the original poster is a known troll.

8 has only one bit set. Clearing that bit, which is necessarily
the least significant set bit, yields 0. Thus, this is correct
behavior.
 
M

Michael Mair

Peter said:
What do you think it does?

If you want to 'test' clc, at least try to be original.
Ack.



No, consider the following (high bit is sign-bit):

1c or 2c(C99) sm
x : 10000000 10000000 10000000 01111111
x - 1: 10000000 01111111 10000000 10000000
& : 10000000 00000000 10000000 00000000

The result could be a trap representation.

Just to make sure: Your "no" is referring to the fact that we
could get a trap representation? Or do you mean that the
formulation "least significant bit" is incorrect?
I am not sure what you want to say with the sign-magnitude example
-- most certainly the "lowest" bit of x is not cleared.

Note also that C89 potentially allows negative representations
beyond 2c, 1c and sm.

Thank you for the reminder (mostly, I assume C99 _restrictions_
for everything and am surprised if C89 is not as strict).


Cheers
Michael
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top