473,698 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13147
> 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).GetTyp e().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**@discussio ns.microsoft.co m> wrote in message
news:49******** *************** ***********@mic rosoft.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).GetTyp e().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
17535
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 18 would be 13 .) I thought this was what the tilde operator (~) did, but when I went to try it I found out that wasn't the case. I discovered by experiment (and verified by looking at the documentation) that the tilde operator takes n to -(n+1)....
2
3466
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 with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
4
1851
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 position "n" to "n+x" where n and x are passed dynamically,where n is start position of the bit from which i will be setting a value and x is the position where I will be finishing the setting.In short it looks like this:
5
13423
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 = Mycalls.ABC | Myclass.XYX; long useThis &= ~doNotUse; bitwise removal of flags not to use How do I convert this to VB.NET? I know to use "OR" instead of " | "
3
12490
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 hair....especially ANSI C's bitwise operators..... For reference i come from the (Java/C#/C++) realm and was never forced to use these. Many people dont understand these....I tried to make sense....I know the truth tables...and I can do simple...
11
2858
by: subramanian | last post by:
Consider the following code: #include<stdio.h> int main(void) { unsigned char c = 0; unsigned int i = ~c;
6
4684
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 VC++ and gcc.
14
10514
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
1884
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 that within 2 bytes(for 16-bit environment) it will be stored as 1000001, but can I find out this data in each bit of these two bytes? 2. How does the bitwise complement (~) operators work with signed int or char? I can understand with unsigned...
0
8608
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.