all groups > c# > march 2004 >
You're in the

c#

group:

Coding this bitwise computation...


Coding this bitwise computation... Michael A. Covington
3/10/2004 11:34:29 PM
c#:
I'm assembling text attribute bytes (upper and lower nybbles) and have the
following line of code:

ushort a = (ushort)((csbi.wAttributes & (ushort)0x0F) |
(ushort)((ushort)value<<4));



Here value is an enum of base type ushort, csbi.wAttributes is a ushort...
so why is everything coverting to int (not even uint) and having to be
converted back?



There has to be a better way to code this!





Re: Coding this bitwise computation... Brad Williams
3/11/2004 8:13:37 AM
<< cannot take ushort:
http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_7_8.asp?frame=true

& cannot take ushort:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_7_8.asp

Looks like ushort will be implicitly converted to int before uint, assuming
the list order is significant on this page:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_6_1_2.asp


[quoted text, click to view]

Re: Coding this bitwise computation... Morten Wennevik
3/11/2004 8:17:43 AM
On Wed, 10 Mar 2004 23:34:29 -0500, Michael A. Covington
[quoted text, click to view]

-- snip --

[quoted text, click to view]
Because int is the smallest type .Net will do calculations on. Possibly
due to speed issues and the use of 32 bit processors.

-- snip --


--
Re: Coding this bitwise computation... Michael A. Covington
3/11/2004 11:38:48 PM
OK, I'll render them all into int (not even uint), do the computation, and
convert to ushort at the end. Thanks to all.


[quoted text, click to view]

AddThis Social Bookmark Button