odd/even bitwise and

  • Thread starter Serve Laurijssen
  • Start date
P

pete

Kevin said:
According to what?

According to the facts that if x is a positve int,
then the representation for x and the representation for 1,
have the same corresponding value bits and the same corresponding
sign bit and the same corresponding padding bits.
And the padding bits aren't part of the value
of either x or 1 or (x & 1).

How do you figure padding bits make a difference ?
 
S

Serve Laurijssen

Barry Schwarz said:
There are systems where bit 0 is the high order or sign bit, not the
low order one.

ok, but what's the bit representation of 1 on such a system then? x & 1
could work on such a system too I'd say.
 
M

Michael Wojcik

You're not missing any representation methods allowed by the C
standard, although I'm sure there are many more outlandish ones
out there.

There are non-outlandish (inlandish?) integer representations which
are not allowed by the C standard, too, such as BCD and various
bignum representations. Or representing integers using a different
byte order than the machine's native one - quite common in COBOL
programs, to maintain binary compatibility with data files generated
by mainframe COBOL programs. The Standard mandates a "pure binary"
representation for good reason (consistent behavior across
implementations), but there are often good reasons to employ other
sorts of representations. It's not in the spirit of C to support
those directly (unlike COBOL, which has evolved by tacking on
features to handle whatever the problem of the moment is), which
is fine, but they're not rare.

Really, it's ones'-complement that few people these days are likely
to encounter.

--
Michael Wojcik (e-mail address removed)

Pocket #9: A complete "artificial glen" with rocks, and artificial moon,
and forester's station. Excellent for achieving the effect of the
sublime without going out-of-doors. -- Joe Green
 
B

Barry Schwarz

ok, but what's the bit representation of 1 on such a system then? x & 1
could work on such a system too I'd say.
The bit representation is still normal binary. The only difference is
the nomenclature assigned to the bits, which is a detail C doesn't
address or need to.

Of course it works under the conditions others have identified. My
comment only addressed the point raised by Ahmed S. Badran that you
snipped which stated that bit 0 is the low order bit. It need not be.



<<Remove the del for email>>
 
P

pete

Barry said:
The bit representation is still normal binary. The only difference is
the nomenclature assigned to the bits, which is a detail C doesn't
address or need to.

Of course it works under the conditions others have identified. My
comment only addressed the point raised by Ahmed S. Badran that you
snipped which stated that bit 0 is the low order bit. It need not be.

Bit 0 isn't mentioned in the standard.
The right bits are low order. The left bits are high order.
 
R

Richard Bos

There are non-outlandish (inlandish?) integer representations which
are not allowed by the C standard, too, such as BCD and various
bignum representations. Or representing integers using a different
byte order than the machine's native one

How is this forbidden? I can't find it in 6.2.6.2.

Richard
 
S

Stephen Sprunk

Chris Torek said:
[using % to obtain integer remainder after division] is the way that
I have always [tested for even/odd], although it occurs to me
that AND-ing a value with one may be a significantly simpler computation
than calculating the remainder of a divide by two in the majority of
circumstances. This method might be worth considering for unsigned
integers.

This is true; but at the same time, on any machine where it matters,
any optimizing compiler worthy of the word "optimizing" should turn:

x % constant
into:
x & (constant - 1)

whenever the given constant is a power of two, because these always
produce the same result (for an unsigned x).

For the record, I just tested gcc 2.91.66 for x86 and it strength-reduces (x
% 2) to (i & 1) even with no optimization enabled.

Thanks for the portability tip -- I'd been "prematurely optimizing" this for
years.

S
 
M

Michael Wojcik

How is this forbidden? I can't find it in 6.2.6.2.

It doesn't appear to be (though it could be argued that it would violate
the spirit of the "pure binary representation" requirement). I added
it in a later edit of the paragraph, without sufficient care. Thanks
for the catch.

BCD and some bignum representations are still good examples, though.
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top