473,325 Members | 2,480 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,325 software developers and data experts.

unchecked() and Bitwise Operations after Promotion

Hi All,

What is wrong with the following? C# claims:
"An unhandled exception of type 'System.OverflowException' occurred in
mscorlib.dll Additional information: Value was either too large or too
small for an unsigned byte."

The offending code is:

// vector< byte >
ArrayList Octets = new ArrayList();
....
Int32 i=0;
while( i < Octets.Count )
{
Byte b = Byte.Parse(Octets[i].ToString());
b &= unchecked(Convert.ToByte(~0x80)); // Problem Here

if( 0x00 == b ) { break; }

....

i++;
}

Frow what I understand, 0x80 is an Int32. operator~ gives me a 32 bit
compliment. unchcked turns off overflow checking, so the cast to a
Byte should work (without overflow complaints)...

Jeff
Jeffrey Walton

Nov 14 '07 #1
5 2615
Hi All,

Forgot to mention... I want to do is check if the high bit is set on
an octet. Sorry about all the extra code. C# does not appear to be
built for byte parsing, such as encountered with ASN.1 parsing.

Jeff

On Nov 14, 9:15 am, Jeffrey Walton <noloa...@gmail.comwrote:
Hi All,

What is wrong with the following? C# claims:
"An unhandled exception of type 'System.OverflowException' occurred in
mscorlib.dll Additional information: Value was either too large or too
small for an unsigned byte."

The offending code is:

// vector< byte >
ArrayList Octets = new ArrayList();
...
Int32 i=0;
while( i < Octets.Count )
{
Byte b = Byte.Parse(Octets[i].ToString());
b &= unchecked(Convert.ToByte(~0x80)); // Problem Here

if( 0x00 == b ) { break; }

....

i++;

}

Frow what I understand, 0x80 is an Int32. operator~ gives me a 32 bit
compliment. unchcked turns off overflow checking, so the cast to a
Byte should work (without overflow complaints)...

Jeff
Jeffrey Walton

Nov 14 '07 #2
On 2007-11-14 06:15:17 -0800, Jeffrey Walton <no******@gmail.comsaid:
[...]
Frow what I understand, 0x80 is an Int32. operator~ gives me a 32 bit
compliment. unchcked turns off overflow checking, so the cast to a
Byte should work (without overflow complaints)...
I try not to bypass the compiler when at all possible, so I might be
missing something here, but...

I believe "unchecked" applies to the _result_ of Convert.ToByte(). The
call to ToByte() itself isn't changed, and of course fails because the
parameter is too large for a byte.

Why can't you just write someting like this:

b &= 0x7f;

or if you really want the ~0x80:

b &= (byte)(~0x80 & 0xff);

or even better, stop messing with b altogther:

if ((b & 0x7f) == 0) { break; }

without the &= line.

I don't see the need to call Convert.ToByte() at all.

Pete

Nov 14 '07 #3
Jeffrey Walton <no******@gmail.comwrote:

<snip>
Frow what I understand, 0x80 is an Int32. operator~ gives me a 32 bit
compliment. unchcked turns off overflow checking, so the cast to a
Byte should work (without overflow complaints)...
That would be the case if you were *casting* to a byte - but you're
not, you're calling a method: Convert.ToByte.

Change the code to:

b &= unchecked((byte)~0x80);

and it'll be fine.

Are you actually in a checked context to start with though? C# is
unchecked by default anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 14 '07 #4
Hi John,

Operator Error. I was not aware the compiler would treat 0x80 as an
integer when the variable operand was a byte. I would have been nice
if it would have demoted for me also.

Add to it the fact that the compliment operator only works on data
types of int and larger, and I'm totally confused.

Jeff

On Nov 14, 2:10 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Jeffrey Walton <noloa...@gmail.comwrote:

<snip>
Frow what I understand, 0x80 is an Int32. operator~ gives me a 32 bit
compliment. unchcked turns off overflow checking, so the cast to a
Byte should work (without overflow complaints)...

That would be the case if you were *casting* to a byte - but you're
not, you're calling a method: Convert.ToByte.

Change the code to:

b &= unchecked((byte)~0x80);

and it'll be fine.

Are you actually in a checked context to start with though? C# is
unchecked by default anyway.

--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 18 '07 #5
Hi Pete,

Operator Error. I was not aware the compiler would treat 0x80 as an
integer when the variable operand was a byte. It would have been nice
if it would have demoted for me also.

Add to it the fact that the compliment operator only works on data
types of int and larger, and I'm totally confused.

Jeff

On Nov 14, 12:43 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com>
wrote:
On 2007-11-14 06:15:17 -0800, Jeffrey Walton <noloa...@gmail.comsaid:
[...]
Frow what I understand, 0x80 is an Int32. operator~ gives me a 32 bit
compliment. unchcked turns off overflow checking, so the cast to a
Byte should work (without overflow complaints)...

I try not to bypass the compiler when at all possible, so I might be
missing something here, but...

I believe "unchecked" applies to the _result_ of Convert.ToByte(). The
call to ToByte() itself isn't changed, and of course fails because the
parameter is too large for a byte.

Why can't you just write someting like this:

b &= 0x7f;

or if you really want the ~0x80:

b &= (byte)(~0x80 & 0xff);

or even better, stop messing with b altogther:

if ((b & 0x7f) == 0) { break; }

without the &= line.

I don't see the need to call Convert.ToByte() at all.

Pete
Nov 18 '07 #6

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

Similar topics

11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
11
by: subramanian | last post by:
Consider the following code: #include<stdio.h> int main(void) { unsigned char c = 0; unsigned int i = ~c;
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.