472,954 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 1214
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Alan | last post by:
I have many OR in the if statement, any method to simplify? if (val == 5 | val == 9 | val == 34 | val == 111 | val == 131 | .......) // .... thank you
29
by: Flzw | last post by:
Alright, here is a simple function I coded, won't be hard understanding what it does, and YES, I know, you will probably tell me it's awful code, I would like to know how to write it better, maybe...
0
by: Stylus Studio | last post by:
Stylus Studio 6 XML Enterprise Edition Now Integrates with TigerLogic XDMS XQuery and Native XML Database Bedford, MA, -- Stylus Studio ( http://www.stylusstudio.com ), the industry-leading...
0
by: Stylus Studio | last post by:
DataDirect XQuery(TM) is the First Embeddable Component for XQuery That is Modeled after the XQuery API for Java(TM) (XQJ) BEDFORD, Mass.--Sept. 20, 2005--DataDirect Technologies...
8
by: ben | last post by:
i have a bit of code, that works absolutely fine as is, but seems over complicated/long winded. is there anyway to shorten/simplify it? the code is below. description of it: it's like strcpy in...
9
by: Russell Mangel | last post by:
Can someone show me how to speed this up? 1. Whats the fastest way for Unsafe C#? 2. What the fastest way for Safe C#? public static Int64 ToInt64(Int32 low, Int32 high) { Byte lowBytes =...
10
by: Russell Mangel | last post by:
Hi, This programs converts a BitString "0101110" to an Int32 value. Can anyone improve, or possibly shorten the following program? public static int BitStringToInt32(String s) { int j, k, m...
3
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own...
1
AmLegacy
by: AmLegacy | last post by:
I'm having a hard time figuring out how to simplify the fractions. Can anyone look at this code and see if you can see something I don't. //This is the fraction adding function void add_fractions...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.