How Should I Do Bitwise Complement with Byte?

G

Guest

I'm trying to set the first three bits to zero on a byte. For some reason,
the compiler is casting the number peculiarly when I use the bitwise
complement operator.

Here's what I think should work but doesn't:

byteArray[6] &= ~0xe0; // set the first three bits to 0 (11100000 == 0xe0)

The error is "Constant value '-225' cannot be converted to a 'byte'".

I've tried several explicit casts, but it seems to only complicate matters
worse. I'd appreciate your suggestions on how to elegantly do this simple
bitwise operation on a byte in C#. (I know...I could do the math in my head
to turn ~0xe0 into 0x1f, but this would make my code less clear in my
opinion.)
 
M

Mike D Sutton

I'm trying to set the first three bits to zero on a byte. For some reason,
the compiler is casting the number peculiarly when I use the bitwise
complement operator.

Here's what I think should work but doesn't:

byteArray[6] &= ~0xe0; // set the first three bits to 0 (11100000 == 0xe0)

The error is "Constant value '-225' cannot be converted to a 'byte'".

I've tried several explicit casts, but it seems to only complicate matters
worse. I'd appreciate your suggestions on how to elegantly do this simple
bitwise operation on a byte in C#. (I know...I could do the math in my head
to turn ~0xe0 into 0x1f, but this would make my code less clear in my
opinion.)

The ~ operator converts the value to an Int32, you can see this if you use type "(~0xE0).GetType().ToString()" into the
immediate window.
You can accomplish what you want by using an XOr instead:

***
byteArray[6] &= (0xe0 ^ 0xff); // set the first three bits to 0 (11100000 == 0xe0)
***

Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi

Under the contrary, IMO ~0xe0 is more difficult to read.

I would use

byteArray[6] &= 0x1F; // 0x1F = 00011111;


the nice comments will help future readers.
 
G

Guest

Thanks for the feedback. Your answer was right on the money about it
implicitly casting it to an int. I dug through the MSDN docs and found the
following:

"The ~ operator performs a bitwise complement operation on its operand,
which has the effect of reversing each bit. Bitwise complement operators are
predefined for int, uint, long, and ulong."

Note that it says nothing about implementing this operator for bytes (or
shorts, for that matter). I wonder why they opted to do that. I haven't
personally noticed any other operators that only work with a select few types.

Mike D Sutton said:
I'm trying to set the first three bits to zero on a byte. For some reason,
the compiler is casting the number peculiarly when I use the bitwise
complement operator.

Here's what I think should work but doesn't:

byteArray[6] &= ~0xe0; // set the first three bits to 0 (11100000 == 0xe0)

The error is "Constant value '-225' cannot be converted to a 'byte'".

I've tried several explicit casts, but it seems to only complicate matters
worse. I'd appreciate your suggestions on how to elegantly do this simple
bitwise operation on a byte in C#. (I know...I could do the math in my head
to turn ~0xe0 into 0x1f, but this would make my code less clear in my
opinion.)

The ~ operator converts the value to an Int32, you can see this if you use type "(~0xE0).GetType().ToString()" into the
immediate window.
You can accomplish what you want by using an XOr instead:

***
byteArray[6] &= (0xe0 ^ 0xff); // set the first three bits to 0 (11100000 == 0xe0)
***

Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top