Click here to Skip to main content
15,881,843 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I am trying to detect the following bits in one byte:

First 8 bit: all set to one (&HFF) - that's quite easy.

But:
9. - 11 bit: also all set to one
12. bit: can be 0 or 1
13. bit: can be 0 or 1
14 - 15 bit: 0-0,0-1,1-0 or 1-1

Does anyone know how I can achieve this (in C# or VB)?

Thanks a lot!



Daniel
Posted
Comments
R. Erasmus 18-Nov-10 1:55am    
In other words:?
bit 14: can be 0 or 1
bit 15: can be 0 or 1
R. Erasmus 18-Nov-10 1:58am    
1.875 bytes to be exact, are you sure this is correct? what about bit 16? Is that a spare?

CSS
9. - 11 bit: also all set to one
12. bit: can be 0 or 1
13. bit: can be 0 or 1
14 - 15 bit: 0-0,0-1,1-0 or 1-1

This means that only the 12 most right bits need to be set. The value for that is
&07ffh, or 2047.

You can check those bits like this:

VB.NET
if ((value And 2047) = 2047) then


C#
if ((value & 2047) == 2047) 


Because you want to check only the 12 most right bits, a bitwise and is performed to discard all other bits because they don't matter. Then you check if the value is equal to make sure that all the bits you need to check are set.

Good luck!
 
Share this answer
 
v2
Comments
E.F. Nijboer 17-Nov-10 18:28pm    
2047 are the 11 most right bits: 0000 0111 1111 1111b
4095 are the 12 most right bits: 0000 1111 1111 1111b

But it looks like you need to check the 11 left most bits, and would need to use the value: 4292870144

Replace the value 2047 with that and try again. You can by the way easily convert binary to decimal using the simple calculator of windows/ubuntu in programmer mode.
How many bits are we talking about here? First you refer to detecting the bits in a byte, then start talking about the 9th thru 12 bit. There are exactly 8 bits in a byte.

As for the problem, read the values into an array of bytes, AND the first byte with 0xff. If that turns out to be equal to 0xff, AND the next byte with 0x07. If that's equal to 0x07, you're done. The pattern you're looking for appears to be xxxxx111 11111111, and it sounds as if the manner of reading it (or at least the way you're testing it) is LSB first. If that's not the case, reverse the order of the tests.

By the way, bits and bytes are traditionally numbered 0 - n, with the least significant being 0. You'll save yourself a whole lot of confusion if you get into the habit of thinking of them that way, as most technical documentation uses this format.

Also, this[^] class comes in handy for these types of problems. Good luck! :-D
 
Share this answer
 
C# is not a good language for lower level programming.

Here is how I will do it in C:

if ((hex_number & 0x7FF) == 0x7FF)
{
  // bits detected
}
else
{
  // bits not detected
}

In C# probebly something like this:
NOTE: & -> bitwise and operator, not a boolean operator!!

if ((hex_number & &H7FF) == &H7FF)
{
  // bits detected
}
else
{
  // bits not detected
}


If its the most significant bits that you want to check then use, &H1FFC00000 instead of &H7FF.

Regards,
Rudolf

P.S. Please Vote!!
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900