471,349 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Bit fiddlers only : Can you simplify this C# code

Hi,

Can someone suggest a better way to implement the GetUInt16() method?
It works okay but I would like to remove the switch() statement if possible.

Thanks,

Russell Mangel
Las Vegas, NV
using System;

class Program
{
static void Main( string[] args )
{
// Bit positions are LSB (least significant bit) as follows: 0,1,2,3,4,5,6,7
// I am interested in bits 4 and 5.
String[] values = { "00000000", "00000100", "00001000", "00001100" };

// Test all four possible values
foreach ( String value in values )
{
UInt16 returnValue = GetUInt16( Convert.ToByte( value, 2 ) );

if ( returnValue != 0xFFFF )
{
Console.WriteLine( returnValue );
}
}

Console.ReadLine( );
}

private static UInt16 GetUInt16( Byte b )
{
// This code works fine, can you simplfy it?
// Is there some way to do this in one or two lines?

UInt16 value = 0xFFFF;

switch ( b & 0x0C )
{
case 0x00:
value = 0; // 00000000
break;
case 0x04:
value = 1; // 00000100
break;
case 0x08:
value = 2; // 00001000
break;
case 0x0C:
value = 3; // 00001100
break;
}

return value;
}
}

Sep 9 '08 #1
16 1147
On Sep 9, 1:02*am, "Russell Mangel" <russ...@tymer.netwrote:
// This code works fine, can you simplfy it?
// Is there some way to do this in one or two lines?
If it ain't broke...don't fix it.

RL
Sep 9 '08 #2
On Sep 9, 9:02*am, "Russell Mangel" <russ...@tymer.netwrote:
Can someone suggest a better way to implement the GetUInt16() method?
It works okay but I would like to remove the switch() statement if possible.
Can't you just shift it right two bits?

Jon
Sep 9 '08 #3
hmmm... my post seems to have been swallowed by NNTP, but the short
version is:

return (ushort)((b >2) & 3);
Sep 9 '08 #4
or:

return (ushort)((b & 0x0c) >2);

"Marc Gravell" <ma**********@gmail.comwrote in message
news:da**********************************@l64g2000 hse.googlegroups.com...
hmmm... my post seems to have been swallowed by NNTP, but the short
version is:

return (ushort)((b >2) & 3);

Sep 9 '08 #5

"Russell Mangel" <ru*****@tymer.netwrote in message
news:eH**************@TK2MSFTNGP06.phx.gbl...
Hi,

Can someone suggest a better way to implement the GetUInt16() method?
It works okay but I would like to remove the switch() statement if
possible.

private static UInt16 GetUInt16( Byte b )
{
// This code works fine, can you simplfy it?
// Is there some way to do this in one or two lines?

UInt16 value = 0xFFFF;

switch ( b & 0x0C )
{
case 0x00:
value = 0; // 00000000
break;
case 0x04:
value = 1; // 00000100
break;
case 0x08:
value = 2; // 00001000
break;
case 0x0C:
value = 3; // 00001100
break;
}

return value;
}
}

private static UInt16 GetUInt16(Byte b)
{
// first checking if passed value is other than 0x0, 0x4, 0x8,
0xC
if ((b & (0xFFFF - 0x0C)) != 0)
{
return 0xFFFF;
}

return (UInt16)(b/4 & 3); // divide by 4 shifts by two bits
right, "& 3" zeroes all bits but the last two.
}
A.

Sep 9 '08 #6
On 9 Sep, 09:54, "pagerintas pritupimas" <o...@com.netwrote:
or:

return (ushort)((b & 0x0c) >2);
Absolutely - but I like to keep it simple... I know that decimal 3 is
binary 11 without checking, but I need to think about numbers like
0x0c

Of course, a comment would help (as per the OP), but then you have to
trust that the comment tells the truth, and isn't out of date due to a
change to flag... "0x18; // 00001100"

Marc
Sep 9 '08 #7
Artur wrote:
* * * * * * // first checking if passed value is other than 0x0, 0x4, 0x8,
good spot... I missed the default case ;-p

Marc

Sep 9 '08 #8
default case will never be executed

"Marc Gravell" <ma**********@gmail.comwrote in message
news:da**********************************@f63g2000 hsf.googlegroups.com...
Artur wrote:
// first checking if passed value is other than 0x0, 0x4, 0x8,
good spot... I missed the default case ;-p

Marc
Sep 9 '08 #9
default case will never be executed

[squints at the original code] - ah, yes; one of the cases will always
match (by exclusion), so the code should *not* have Artur's 0xFFFF
test... I should have stuck with my original answer ;-p

Marc
Sep 9 '08 #10
Can't you just shift it right two bits?
Jon

Yes, I tried ( ushort )( ( b >2 ) but that only shifts bits 4 and 5 to the
end,
if the Byte had '1's in bit postion 0-3 you will get those as well. I need
just
bit's 4 and 5.

I got several reply's on this, and the &3 is what I missed.

Thanks for your reply.

Russ
Sep 9 '08 #11
>
return (ushort)((b >2) & 3);
Yeah, this is what I couldn't get working, I didn't know how
to right shift only bits 4 and 5.

Thanks for helping.

Russ
Sep 9 '08 #12
>
return (ushort)((b & 0x0c) >2);

"Marc Gravell" <ma**********@gmail.comwrote in message
>>
return (ushort)((b >2) & 3);
Thanks for your help, I was going nuts trying to get this working.

Let's see if I can read the difference between these to working solutions.

Your solution isolates bits 4 and 5 and then shifts them 2 positions.
Marc's solution shifts everything 2 positions and then isolates the last 3
bits.

Do I have that right?

Russ
Sep 9 '08 #13
>
return (UInt16)(b/4 & 3); // divide by 4 shifts by two bits right, "& 3"
zeroes all bits but the last two.
A.
Interesting, any reason that you suggest b/4 instead of shifting two bits >>
2.
I don't understand why b/4 works...

Thanks

Russ
Sep 9 '08 #14
Marc's solution shifts everything 2 positions and then isolates the last 3
bits.
last 2 bits; decimal 3 = binary 0000...00011

Other than that, correct.

Marc
Sep 9 '08 #15
Dividing a binary number by b'10' (or decimal 2) is equivalent to shifting
it right by 1. Dividing a number by 2 and then dividing by 2 again is
equivalent to diving by 4 or shifting it right twice. Hope that makes sence
:]

"Russell Mangel" <ru*****@tymer.netwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...

return (UInt16)(b/4 & 3); // divide by 4 shifts by two bits right, "& 3"
zeroes all bits but the last two.
A.

Interesting, any reason that you suggest b/4 instead of shifting two bits
>2.
I don't understand why b/4 works...

Thanks

Russ

Sep 9 '08 #16

"pagerintas pritupimas" <or*@com.netwrote in message
news:uk**************@TK2MSFTNGP04.phx.gbl...
Dividing a binary number by b'10' (or decimal 2) is equivalent to shifting
it right by 1. Dividing a number by 2 and then dividing by 2 again is
equivalent to diving by 4 or shifting it right twice. Hope that makes
sence :]
Okay, I see your thinking now. Thanks again for you input.

Russ
Sep 9 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by Alan | last post: by
29 posts views Thread by Flzw | last post: by
AmLegacy
1 post views Thread by AmLegacy | last post: by

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.