Velocity Reviews > odd/even bitwise and

# odd/even bitwise and

Serve Laurijssen
Guest
Posts: n/a

 04-06-2004
Some people prefer to use
"if (x & 1)"
to see if a number is odd or even. Is this completely portable according to
the standard?

Guest
Posts: n/a

 04-06-2004

"Serve Laurijssen" <(E-Mail Removed)> wrote in message
> "if (x & 1)"
> to see if a number is odd or even. Is this completely portable according

to
> the standard?

I don't know what does this have to do with the standard, but the thing is
an odd number will ALWAYS have bit 0 set to '1' and an even number will
always have bit 0 set to '0'. This is a matter of binary representation.

Ahmed

CBFalconer
Guest
Posts: n/a

 04-06-2004
Serve Laurijssen wrote:
>
> Some people prefer to use
> "if (x & 1)"
> to see if a number is odd or even. Is this completely portable
> according to the standard?

Only if x is some form of unsigned integer.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

pete
Guest
Posts: n/a

 04-06-2004
Serve Laurijssen wrote:
>
> Some people prefer to use
> "if (x & 1)"
> to see if a number is odd or even.
> Is this completely portable according to the standard?

If x is an unsigned integer type
or a signed type with a non negative value, then it's portable.

--
pete

pete
Guest
Posts: n/a

 04-06-2004
>
> "Serve Laurijssen" <(E-Mail Removed)> wrote in message
> > "if (x & 1)"
> > to see if a number is odd or even.
> > Is this completely portable according to the standard?

>
> I don't know what does this have to do with the standard,

The answer to the question, is in the standard.

> but the thing is an odd number will ALWAYS have bit 0 set to
> '1' and an even number will always have bit 0 set to '0'.

That's not true for one's complement
representations of negative integers.

> This is a matter of binary representation.

The standard specifies more than one
way to represent negative integers.

--
pete

osmium
Guest
Posts: n/a

 04-06-2004

> > "if (x & 1)"
> > to see if a number is odd or even. Is this completely portable according

> to
> > the standard?

>
> I don't know what does this have to do with the standard, but the thing is
> an odd number will ALWAYS have bit 0 set to '1' and an even number will
> always have bit 0 set to '0'. This is a matter of binary representation.

It depends on the hardware. If x is a one's complement representation of 0
the test might fail. It strikes me as code intended to impress someone with
one's erudition, I would not do it.

Guest
Posts: n/a

 04-06-2004
pete wrote:
>>
>> "Serve Laurijssen" <(E-Mail Removed)> wrote in message
>>> "if (x & 1)"
>>> to see if a number is odd or even.
>>> Is this completely portable according to the standard?

>>
>> I don't know what does this have to do with the standard,

>
> The answer to the question, is in the standard.
>
>> but the thing is an odd number will ALWAYS have bit 0 set to
>> '1' and an even number will always have bit 0 set to '0'.

>
> That's not true for one's complement
> representations of negative integers.
>
>> This is a matter of binary representation.

>
> The standard specifies more than one
> way to represent negative integers.

Ok, just to re-elaborate and make the answer complete, I never took negative
numbers into consideration with my previous answer, so my previous answer is
valid/correct with all positive integers. Thanks for pointing that out guys.

Ahmed

=?ISO-8859-1?Q?Rog=E9rio_Brito?=
Guest
Posts: n/a

 04-06-2004
Serve Laurijssen wrote:
> Some people prefer to use
> "if (x & 1)"
> to see if a number is odd or even. Is this completely portable according to
> the standard?

No. That is only possible if the representation of the integers use
twos-complement.

To see if a number is odd or even, use the modulus operator (e.g., x%2).

--
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Rogério Brito - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://www.ime.usp.br/~rbrito
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Mark Henning
Guest
Posts: n/a

 04-06-2004
Rogério Brito wrote:
> No. That is only possible if the representation of the integers use
> twos-complement.
>
> To see if a number is odd or even, use the modulus operator (e.g., x%2).

That is the way that I have always done it, 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.

M. Henning.

Chris Torek
Guest
Posts: n/a

 04-06-2004
In article <news:c4ukgf\$2ce\$(E-Mail Removed)>
Mark Henning <(E-Mail Removed)> writes:
>[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 signed integers (and still power-of-two constants), the process
is a bit more difficult -- a typical two's complement signed integer
gives different answers for "x % CONST" and "x & (CONST-1)" when
x is negative. There are bit-twiddling tricks that can be used if
the value is required, though; and if only the "truth-ness" of the
value is of interest, the above transform again works. That is:

if (x %

and:

if (x & 7)

are both true (or false) in the same sets of cases, even when x is
signed, as long as the machine uses two's complement. This allows
an optimizing compiler to "do the right thing".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
Reading email is like searching for food in the garbage, thanks to spammers.