extracting info from bit patterns

closed account (SECMoG1T)
hello everyone, how would one tell if a number is even or odd by inspecting it's bits pattern, i really need this info

Thanks.
Yolanda
Why don't you print a few even and odd numbers as binary and see if there is a pattern.
std::bitset might be a help. http://www.cplusplus.com/reference/bitset/bitset/
Well my obligatory response is to not do it that way, since the compiler will know how to optimize the normal way of checking if a number is odd or even, by finding its remainder (mod 2).

1
2
3
4
bool isEven(int n)
{
    return n % 2 == 0;
}


That being said... to do that you need to use bitwise "AND" logic, by checking the number against 1 (0...0000001 in binary) and seeing whether the result is 0.
In other words, you're checking if the least-significant bit is: 0 if even, 1 if odd.


(Edited after reading a post by JLBorges)
1
2
3
4
bool isEven(int n)
{
    return abs(n) & 1 == 0;
}


ex:
1
2
3
4
    1 0 1 1 // 11
AND 0 0 0 1 // 1
    ______
    0 0 0 1 // 1, therefore odd. 
Last edited on
Even/odd is straightforward. Look at a few numbers in decimal/binary

 0    000
 1    001
 2    010
 3    011
 4    100
 5    101
 6    110

You only need to examine the rightmost bit (the least significant bit). If it is zero, the number is even. If it is 1, the number is odd.

You can test that using a bit mask and the bitwise AND operator.

1
2
    if (n & 1)
        cout << n << " is odd";

Here, the value 1 is termed a mask, as it is used to allow us to mask out the bits we are not interested in, and reveal the bits which we are interested in.

Tutorial:
https://www.cprogramming.com/tutorial/bitwise_operators.html
closed account (SECMoG1T)
thank you very much guys @Thomas1965 @Ganado , it's to unfortunate that i have to do this on bare metal so i'll not have access to this libraries and functions, the only thing i have are buffers
similar to arrays some counting and looping functionalities,just that, this buffers are to hold chucks of binary stream

what i would love to know is, is there a way i would tell if a chunk 16bits is even / odd
i din't think it would be quite a hustle

EDIT: i have the capacity to examine this buffers bit by bit using this loops and i thought there
would be a way to tell by examining this chunks if they're even or not.
Last edited on
A chunk of 16 bits is just a chunk of 16 bits. It doesn't have a concept of even or odd.

Are you asking about the count of how many bits are set 'on' and how many set 'off'?

The std::bitset has a member function count() which does that. Even though you may not be officially allowed to use it, it can be valuable as a way of cross-checking your results.
Last edited on
Err, we had a bit of fun with this some time back. I went for rightmost bit, but @JLBorges had to put me straight.
http://www.cplusplus.com/forum/beginner/207434/#msg979140
Last edited on
if you can loop over the bits, you can look at the least significant bit.
if you can look at the least bit, you can check it vs being 1 or 0. 1 is odd. 0 is even. This bit represents 2 to the 0th power so it is either set (2 to the 0 which is 1, which is odd) or not set (value is 0, its even).

Which is just a rehash of what was said here in various ways already.

most data in c++ is stored with a byte as the smallest chunk. You have a 2 byte chunk. It makes sense to do the built-in bit logic to a 16 bit integer (as said above) or %2 (which works without any libraries). Arrays of bytes that each represent a bit is a terrible waste of space. IIRC vector bool is stored bit-wise if possible. And bitsets or bitfields as stated will do it, but I find those clunky for most uses (they work great on oddball hardware though).
Last edited on
closed account (SECMoG1T)
thanks everyone , i'll be trying @jonnin's and @Chervil suggestion .

for those wondering why i have to check for this even odd stuff, when this device malfunctions
it'll have to reconfigure itself, 5 chunks of 16bits {error codes, but can only be stored in bits} are set periodically, there are about 110+ codes, i have noted all error codes are even numbers so instead of looping through this codes converting them to bits and comparing them to my buffers, i thought i could just check if a chuck is even, switch control to a standby device, restart the failing module and kaboom mission complete.

Yolanda
Last edited on
closed account (SECMoG1T)
thank yah everyone:
if you can look at the least bit, you can check it vs being 1 or 0. 1 is odd. 0 is even
this solved my problem.

i see:

even numbers have LSB == 0
odd   numbers have LSB == 1

 0    000
 1    001
 2    010
 3    011
 4    100
 5    101
 6    110


am sure i wasn't quite clear, i din't mention that i couldn't use c++ lib because that would be too big to store in such a small storage, now am running fine anyways.
Last edited on
Topic archived. No new replies allowed.