I have to return 1 if all the even bits in word are set to 1. The trouble for me is I can only use logical operators like + , >> , <<, |, ^, ~ , !
. No if's
or for
loops. I thinking I would want to mask it using 0x55555555
but that's where I get stuck. And its assumed this is 32bit
.
Asked
Active
Viewed 147 times
-4

user2736738
- 30,591
- 5
- 42
- 56

user3769402
- 201
- 1
- 4
- 11
-
Stackoverflow is not here to right code for you. You present what you have if you are having problems but we don't write code for you. – Rob Apr 19 '15 at 00:22
-
@Rob: He presented what he has. "I thinking I would want to mask it using 0x55555555 but that's where I get stuck." That's actually a pretty good start. – user2357112 Apr 19 '15 at 00:24
-
Which bits do you consider to be the "even" bits? – user2357112 Apr 19 '15 at 00:26
-
3@Rob: Progress doesn't have to be code. – user2357112 Apr 19 '15 at 00:27
-
So if it comes back 01010101010101010101010101010101 then return 1. And say that equal int = y. So I'm assuming if I do a (x & y) that would give me what I want? – user3769402 Apr 19 '15 at 00:27
-
@user3769402: Mostly. It's not quite `return (x & y)`, but you can test whether the result `(x & y)` gives you is equal to what you expect. Make sure you know the specific numbers `==` gives you if the operands are or aren't equal. – user2357112 Apr 19 '15 at 00:33
-
Alright I got the mask working because it returns 0x55555555 from here its just comparing to the even values of x. – user3769402 Apr 19 '15 at 00:36
-
@user3769402 Can you use the == operator? – Corb3nik Apr 19 '15 at 00:38
-
And "even" bits – starting with 0 or 1? – user3125367 Apr 19 '15 at 00:40
-
If we may assume that a word is 32 bits: return( ((x & 0x55555555)==0x55555555) ); ... There's a test, but no if! – Sir Jo Black Apr 19 '15 at 00:43
-
possible duplicate of [Checking even or odd \`1\` bits in a number](http://stackoverflow.com/questions/12876067/checking-even-or-odd-1-bits-in-a-number) – David C. Rankin Apr 19 '15 at 00:44
-
Another solution may be: return ( !((x & 0x55555555)^0x55555555) ) – Sir Jo Black Apr 19 '15 at 00:46
-
@David C. Rankin, this is different because there's a constraint! – Sir Jo Black Apr 19 '15 at 00:52
-
So according to you when the answer given `((x ^ (x >> 1)) + 1) & (x ^ (x >> 1)) == 0` shows precisely how to determine if all even/odd bits are on within a number of any size meeting this questioner's constraints, that is somehow different from finding whether all even bits are on in a number here?? One goal of SO is to avoid having the same, or negligibly differing, question answered over and over again with negligibly differing answers scattered across the site. "Thinking about reducing the solution to a mask" does not make the question fundamentally different. – David C. Rankin Apr 19 '15 at 07:52
-
@DavidC.Rankin. I tried to use the function you indicate with x equal to: 0x55555555, 0x555F5555 and 0x55545555. The only case in wich returns 0 is 0x55555555, but also 0x555F5555 has all the even bits set! Moreover "==" is not a logical operator! – Sir Jo Black Apr 20 '15 at 19:55
1 Answers
1
For a 32 bits word:
#include <stdint.h>
int allBitEven(uint32_t x)
{
return ( !((x & 0x55555555)^0x55555555) );
}
Since I'm playing with this question, I propose another function that may verify if all bits are odd or all bits are even only with logical operations:
int allBitEvenOrOdd(uint32_t x, int odd)
{
return ( !((x & (0x55555555<<odd))^(0x55555555<<odd)) );
}
if odd is 1 it verifies if all bits are odd, if odd is 0 it verifies if all bit are even!

Sir Jo Black
- 2,024
- 2
- 15
- 22
-
Add an `odd &= 1;` and you'll gain a +1 for thinking outside the box! – Jongware Apr 19 '15 at 09:14
-
@Jongware. Good point, I agree very much! Yes, It's a good idea ... I was thinking about this, but I preferred to leave the function less complex! – Sir Jo Black Apr 19 '15 at 17:54
-
It may be important to point out that it will *only* work with a single shift, not more than that. – Jongware Apr 19 '15 at 17:56
-
I think that this is quite a playing ... The real result is what the compiler produce! Our "glory" is to have found a logical solution. Simply a good exercise! – Sir Jo Black Apr 19 '15 at 18:01