473,378 Members | 1,416 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How Should I Do Bitwise Complement with Byte?

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.)
Jun 21 '06 #1
3 13086
> 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: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Jun 21 '06 #2
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.

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marc" <Ma**@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
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.)

Jun 21 '06 #3
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" wrote:
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: ED***@mvps.org
WWW: Http://EDais.mvps.org/

Jun 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Elaine Jackson | last post by:
Is there a function that takes a number with binary numeral a1...an to the number with binary numeral b1...bn, where each bi is 1 if ai is 0, and vice versa? (For example, the function's value at...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
4
by: s.subbarayan | last post by:
Dear all, I would like to know the easiest efficient way to set or inject a particular value in the given word or byte?The problem is: I have to implement a function which will set a value from...
5
by: Bill Dee | last post by:
I need help converting a tiny piece of code which uses the bitwise complement operator from C# to VB.NET. In C# I currently have this: long useThis = Myclass.ALLCONSTANTS; long doNotUse =...
3
by: shdwsclan | last post by:
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my...
11
by: subramanian | last post by:
Consider the following code: #include<stdio.h> int main(void) { unsigned char c = 0; unsigned int i = ~c;
6
by: subramanian | last post by:
Suppose I have the following statement: unsigned char x = 0; If I do, printf("%u\", ~x); it prints the value of UINT_MAX. I am using Intel machine. This same result is printed in both...
14
by: darthghandi | last post by:
What would be the most efficient way to calculate the two's complement of a variable length byte array? Thanks for your time.
2
kirtikjr
by: kirtikjr | last post by:
1. Can I access each bit of value stored in a byte? Supposing an integer value 65 (int i =65;) is stored in 2 bytes of memory, I can easily find out its memory location by using pointers. We know...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.