|
Hi,
shifting is the key.
byte b = 0xaa;
if ((b && 0x01) > 0)
{
}
else
{
}
if (((b >> 1) && 0x01) > 0)
{
}
else
{
} etc.
bye
|
|
|
|
|
It looks like a good idea i just cant get it to work as i cant compare the byte to 0x01
|
|
|
|
|
Hi,
yes, I'm sorry. I've made a little fault: not (byte1 && 0x01) > 0 but (byte1 & 0x01) > 0
sorry
|
|
|
|
|
It's a matter of using ANDs, and other boolean stuff (if you want to read multiple bits). Basically, you use the formula:
bitNSet = (originalInteger & (1 << N) == 1 << N)
Effectively, every integer is represented by a binary sequence. For example, 39 would be represented by 100111. Each one and zero is a bit. To get a bit, you have to AND it, which gets all the bits which are set in both of the numbers given to it. For example, 39 AND 4 would be worked out like so:
100111 AND
000100 =
------
000100
As you can see, you end up with a binary sequence, which is represented by 4 in decimal (base-10). You use the AND against a bit pattern to see if that bit is set. Now, unfortunately, you don't get a true or false value. Note that the result of 39 AND 4 was 4. So, we can apply this further, and say that if q AND n is equal to n, then bit n was set.
Now we just have to get a binary pattern to use against the AND. To get this, we just use the << operator. It will perform a left binary shift on the left operand. So 1 << 5 would result in 100000, 1 << 2 would result in 100, etc. Those numbers are in binary format by the way. So, shifting 1 left by N will give us a binary pattern where only the Nth bit is set. By ANDing that with the original integer, we can check if the bit is set
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
In addition to the excellent advice you've been given - all the Boolean logical operator variants are easy to implement as shown below. Also, using an enum with the Flags attribute can make getting individual bits easier as you can give your enum members more descriptive names.
class Program
{
static void Main(string[] args)
{
byte baseValue = 0x01;
byte otherValue = 0x07;
Console.WriteLine("Base Value = {0} : Other Value = {1}", baseValue, otherValue);
Console.WriteLine("AND result = {0}", baseValue & otherValue);
Console.WriteLine("OR result = {0}", baseValue | otherValue);
Console.WriteLine("XOR result = {0}", baseValue ^ otherValue);
Console.WriteLine("NOT result (Base only) = {0}", (byte)~baseValue);
Console.WriteLine("NAND result = {0}", (byte)~(baseValue & otherValue));
Console.WriteLine("NOR result = {0}", (byte)~(baseValue | otherValue));
Console.WriteLine("Bit0 of Base Value = {0}", baseValue & (byte)Bits.Bit0);
Console.WriteLine("Bit0 and Bit1 of Other Value = {0}", otherValue & (byte)(Bits.Bit0 | Bits.Bit1));
Console.ReadKey();
}
}
[Flags]
public enum Bits : byte
{
None = 0,
Bit0 = 1,
Bit1 = 2,
Bit2 = 4,
Bit3 = 8,
Bit4 = 16,
Bit5 = 32,
Bit6 = 64,
Bit7 = 128
} If you want to work with binary values directly, unfortunately there is no binary literal in C#. I made this article[^] quite a while ago to make it easier to work with binary strings, you may find it of some use.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
10!
This is what I would suggest.
|
|
|
|
|
Hi,
this code works like a bit getter on a byte array:
public static bool bitGetter(byte[] bytes, int index) {
int byteNumber=index/8;
int bitNumber=index%8;
int bitMask=1<<bitNumber;
int byt=bytes[byteNumber];
int bit=byt&bitMask;
if (bit==0) return false;
return true;
}
and the condensed version is:
public static bool bitGetter(byte[] bytes, int index) {
return 0!=bytes[index/8]&(1<<(index%8));
}
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
How to find the needless references in a project ?
I don't sure that everybody can understand my question, so I just made a sample:
1. create a c# sln in VS
2. add 3 classLib (named lib1,lib2,lib3) project into the sln
3. lib1 called some classes or functions which in lib2
4. for lib1, lib3 is useless.
My question is : how to find the needless references?( lib3 )
Is anybody can help me ?
i love star in the sky,i wish she will be seeing me forever...
|
|
|
|
|
I use resharper to do it, just click at "Find Usage..."
|
|
|
|
|
hai friends,
I want to play the live recording or video of a particular ip web cam when i give the particular ip address using C#.Net Windows application. How to send a live video through ip network and how get the remote video in a windows application.
by
Vasanth.A
|
|
|
|
|
IP Cameras usually have a web interface. If yours does then you can just use a web browser control to browse to the video feed. If it doesn't then you'll need to examine the cameras API to determine how to get the video off of it.
|
|
|
|
|
I recently ran across a cute snippet that detected the type of an object and converted another object to that type. All this in 1 line of code and I lost it, cannot find it for the life of me. Anyone have any ideas on this one.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
Yes
Basically I have a propertyinfo and a datacolumn and want to populate the property with the content of row[0] of the datacolumn. I think Piebalds ChangeType will do the trick.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I dunno, something like: System.Convert.ChangeType ( typeof(x) , y ) ?
|
|
|
|
|
PIEBALDconsult wrote: ChangeType
thats the one, thank you very much, found the script
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I find it's the only worthwhile member of Convert.
|
|
|
|
|
How about the base64 things? Are they bad? I've used them in the past, but if you say it's better to write your own I'll rewrite those things of course
|
|
|
|
|
Possibly, haven't used them, and I don't like the choice of plus and minus as digits. Makes base64 math hard to read.
I wrote my own.
|
|
|
|
|
Ok thanks I'll keep that in mind
|
|
|
|
|
|
Sure, just write some code.
|
|
|
|
|
|
Unfortunately, your question betrays a level of ignorance of basic C# coding that makes us assume the code you're posting came from the web, and you have no idea what it does. Perhaps you should read a book as your introduction to C#.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
You're using the wrong container to start with. If you want to store name/value pairs, use a dictionary to do it, or create a struct to contain the values. If you used a dictionary, then you could use FNAME and LNAME as keys and look the values up by key.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|