Cadab Cadab - 1 month ago 5x
C# Question

Binary AND Operations

I'm trying to do some Binary AND operations in c# based on MSDN article about & Operator

if i do:

1111 & 10 = 2 (0010) // Which is what i expect


1111 & 100 = 68 (1000100) // Which is **not** what i expect.

I would have thought the output would have been 100

enter image description here

What am i missing?


The numbers you specified, 1111 and 100, are being treated as base 10 numbers, not binary numbers.

If you want the binary integer 1111 you should enter 15, as that's the base 10 version. So:

15 & 4 will become 4, as expected.

There is no syntax in C# for specifying an integer literal in binary. You don't have any choice but to convert them into base ten yourself, or use a runtime-conversion such as through Convert.ToInt.