http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

Wise2Food



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Least significant bit?



Assuming you are looking at positive and negative number you can't check 
just one bit. If you are using unsigned numbers, or even numbers that you 
will guarantee are always positive, you should be able to do a bitwise and 
on your unsigned number and the number one like this (C code):

unsigned int TestValue = ??????;
unsigned int One = 1;

if (TestValue & One) { // a single ampersand is a bitwise AND
//Odd
} else {
//Even
}

A problem arises if you have negative numbers, In order to have only one 
Zero value, zero has the value (00000000) as would be expected with the 
first bit being a sign bit (irrelevant with Zero) and the last seven bits 
containing the value. One would obviously be (00000001) but Negative one is 
(10000000) and negative 2 is (10000001) . Basically the negative number is 
bitwise the equivalent to it's positive counterpart - 1 with the sign bit 
flipped.

Jeff

----Original Message Follows----
From: Lionel Pinkhard <lionelp@worldonline.co.za>
Reply-To: gameprogrammer@gameprogrammer.com
To: gameprogrammer@gameprogrammer.com
Subject: Least significant bit?
Date: Wed, 26 Jul 2000 11:18:11 +0000 (UTC)

Hi,

Can somebody tell me how to check the least significant bit (bit 0) of a 
number?

What I'm trying to do is only to check whether a given number is even or 
odd, and (please do correct me if I calculated incorrectly) I think that all 
even numbers have 0 as the least significant bit, and all odd numbers have 
1, am I correct? Also, is this
  the best/fastest way to do this kind of calculation, or is there something 
better?

I've been thinking of doing this with some kind of AND/OR/XOR mask, but I 
think that would get way too complex (after all, I'm only trying to read in 
ONE BIT!)

Kind regards,

Lionel Pinkhard

=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once

To SUBSCRIBE or UNSUBSCꫨ please visit:
http://gameprogrammer.com/mailinglist.html

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com

=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once

To SUBSCRIBE or UNSUBSCꫨ please visit:
http://gameprogrammer.com/mailinglist.html