Numbers prefixed with 0 in C are not in base-2 (binary), but base-8 (octal) In Python, the same can be achieved by prefixing the number with 0o
Python 3.5.3 (default, Jan 19 2017, 14:11:04)
>>> (55 & 0o00000111) + 1
2
>>> 0o00000111
73
>>> 1 * 8 ** 2 + 1 * 8 ** 1 + 1 * 8 ** 0
73
C does not support binary integer constants. However, as an extension, GCC does, so
#include <stdio.h>
int main()
{
printf("%u\n", (55 & 0b00000111) + 1);
return 0;
}
Can be compiled with GCC:
% gcc bin.c && ./a.out
8
As others have pointed out, hex is much more convenient than binary anyway - you just need to remember how each of the hex digit 0-F looks in binary, and replace groups of 4 bits with a single hex digit:
0000 | 0111
0 7
---> 0x07
And unlike binary notation, this works in all C compilers and Python versions alike.