3

I'm finishing up some CSE homework and I have a quick question about declaring integers of larger bit sizes. My task is to implement a function that returns 1 if any odd bit of x is 1 (assuming size of x is 32 bits) and returns 0 otherwise.

Am I allowed to declare an integer with the bit value:

10101010101010101010101010101010

If so, are there any problems that could arise from this? If not, why not?? What alternatives do I have?

My function:

int any_odd_one(unsigned x)
{
    int mask = 10101010101010101010101010101010
    if(x & mask)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

Thanks in advance for any assistance!

-Matt

Matt Koz
  • 67
  • 3
  • 10
  • 6
    Use hex - `0xaaaaaaaa`? – nneonneo Oct 26 '12 at 22:37
  • 2
    Use `unsigned int` as the type for `mask`. Using `int` is asking for trouble. – Pascal Cuoq Oct 26 '12 at 22:39
  • 2
    [Google](https://www.google.com/search?q=0b10101010101010101010101010101010+in+hex) was my standard base converter in school – NullUserException Oct 26 '12 at 22:39
  • you need the "0b" prefix to the binary – HRÓÐÓLFR Oct 26 '12 at 22:40
  • 1
    @mikhailvs and a compiler that eats that nonstandard `0xbadf00d`. –  Oct 26 '12 at 22:41
  • 1
    If your assignment allows it ... and if the assignment assumes 32 bits (or lower) ... then a mask is definitely the way to go. Your mask happens to be incorrect: I'd specify "unsigned long" (not "int"), and I'd specify "0xaaaaaaaa". IMHO... – paulsm4 Oct 26 '12 at 22:44
  • @paulsm4 better `uint32_t` if you have C99. –  Oct 26 '12 at 22:44
  • `6 & 0xaaaaaaaa = 2` and `0xaaaaaaaa & 5 = 0` is this what you want ? – yeyo Oct 26 '12 at 22:46
  • @PascalCuoq Could you explain why a signed int is asking for trouble? – Mattwmaster58 Nov 17 '21 at 16:10
  • @Mattwmaster58 In C, unsigned integers can only be represented in binary, whereas signed integers can be represented in any of: 2's complement, 1's complement or sign-magnitude. The bitwise operations reveal the details of the representation, for instance when `x` has type `int`, `x&(-1)` will not do what you are used to if the representation is sign-magnitude. This is largely theoretical nowadays as 99.99% of architectures use 2' complement, but the left shift operation is also UB for a negative first operand, originally because the representation of negative values can vary. – Pascal Cuoq Nov 22 '21 at 09:20
  • @Mattwmaster58 In short, if some integers are viewed as vectors of bits, it is safer to use an unsigned integer type to store these, and while on this topic, remember to make sure that `e` has already the intended width before computing `~ e`. – Pascal Cuoq Nov 22 '21 at 09:24

2 Answers2

7

You can't use binary literals in C. Instead, use hexadecimal or octal notation.

In your case, you'd use unsigned mask = 0xaaaaaaaa since 10101010... is 0xaaaaaaaa when expressed in hexadecimal (each 1010 is a in hex).

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Isn't that possible to keep binary using 0b10101010101010101010101010101010 in c programming? – Tajuddin Khandaker Apr 01 '20 at 06:56
  • 1
    GCC supports binary literals in C as a *GNU extension*, but they aren't standard C. C++14 standardized binary literals, but they aren't in any standard of C yet AFAIK. – nneonneo Apr 01 '20 at 19:35
3

It is more fun to implement this as return !!(x&-1u/3*2);.

In addition to the integer width stated in the problem, it works for any even number of bits in the unsigned type.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312