Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > odd/even bitwise and

Reply
Thread Tools

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?


 
Reply With Quote
 
 
 
 
Ahmed S. Badran
Guest
Posts: n/a
 
      04-06-2004

"Serve Laurijssen" <(E-Mail Removed)> wrote in message
news:eJucc.5394$RU5.83970@zonnet-reader-1...
> "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


 
Reply With Quote
 
 
 
 
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.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      04-06-2004
Ahmed S. Badran wrote:
>
> "Serve Laurijssen" <(E-Mail Removed)> wrote in message
> news:eJucc.5394$RU5.83970@zonnet-reader-1...
> > "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
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      04-06-2004
Ahmed S. Badran writes:

> > "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.


 
Reply With Quote
 
Ahmed S. Badran
Guest
Posts: n/a
 
      04-06-2004
pete wrote:
> Ahmed S. Badran wrote:
>>
>> "Serve Laurijssen" <(E-Mail Removed)> wrote in message
>> news:eJucc.5394$RU5.83970@zonnet-reader-1...
>>> "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



 
Reply With Quote
 
=?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
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

 
Reply With Quote
 
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.


 
Reply With Quote
 
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
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bitwise AND operation in xslt biswaranjan.rath XML 3 11-12-2008 03:14 PM
Bitwise AND overloading and argument list mweltin@gmail.com C++ 1 05-15-2007 12:13 AM
Re: Odd Perl bitwise-AND & MySQL problem? dohnut Perl 0 10-21-2003 03:55 AM
Re: Odd Perl bitwise-AND & MySQL problem? dohnut Perl 1 10-21-2003 03:46 AM
Odd Perl bitwise-AND & MySQL problem? dohnut Perl 0 10-20-2003 09:26 PM



Advertisments