I know that the bitwise AND of 8 and 4 will return 0 or false and the
bitwise AND of 8 and 9 will return 1 or true but I don't know how to write
the synax for it in C#. I have a value that ranges from 0 to 15 and I need
to compare it to 15 in order to find if it contains the values 1, 2, 4, or
8.
To represent it more graphically, the value 1010 when ANDed with 1000 or
0010 will produce either true or a value greater than 0. I believe the
operator I need to use is &. Can anyone tell me how this is done? 9 7008
Christopher,
if ((value & 0x01) == 0x01)
Contains1();
if ((value & 0x02) == 0x02)
Contains2();
etc.
Regards - Octavio
"Christopher Weaver" <we*****@nospamverizon.net> escribió en el mensaje
news:%2****************@tk2msftngp13.phx.gbl... I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8.
To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe the operator I need to use is &. Can anyone tell me how this is done?
This works where Status is the value containing the various options and
STATUS_CANCELLED is a constant that represents one of them:
if ((Status & STATUS_CANCELLED) >0)
{
ckbCancelled.Checked = true;
}
else
{
ckbCancelled.Checked = false;
}
Any other ideas?
"Christopher Weaver" <we*****@nospamverizon.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8.
To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe the operator I need to use is &. Can anyone tell me how this is done?
Actually, 8 AND 9 gives 8 not 1.
AFAIK, for the CLR 0 is FALSE and anything NOT FALSE is TRUE (ie.
anything not zero)
i.e.
System.Convert.ToBoolean(8) = True
So 8 AND 9 is true but becuase it is not equal to 0, not because it is
equal to 1.
Back on topic: the bitwise AND and OR operators are & and |
8 AND 9 ==> 8 & 9
for any bit test
(( value & bit) == bit)
e.g.
((15 & 4) == 4) is true, thus 4 is turned on
((9 & 4) == 4) is false, thus 4 is turned off
The simplest way to do this is to set up a lookup table.
bool[][] lookup = new bool[16][]{ new bool[4]{false,
false,false,false},
new bool[4]{false,
false,false,true},
...
new bool[4]{true,
true,true,true}};
Indexing into the table e.g. lookup[9] will give you a bool[4]
indicating which bits are turned on.
hth,
Alan.
Christopher Weaver <we*****@nospamverizon.net> wrote: I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8.
To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe the operator I need to use is &. Can anyone tell me how this is done?
int x = 8 & 9; // x=1
int y = 8 & 4; // y=0;
Does that help?
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] <sk***@pobox.com> wrote: int x = 8 & 9; // x=1
Oops - as has been pointed out, of course, x=8.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Just as an added measure, you might want to use a flag enum in your
code to make this code section more readable.
private void Form1_Load(object sender, System.EventArgs e)
{
int someValue = 7;
Settings settings = (Settings) someValue;
if (settings == Settings.None)
{
MessageBox.Show("None");
}
if ((settings | Settings.Setting1 | Settings.Setting2 |
Settings.Setting3 | Settings.Setting4) > 0)
{
MessageBox.Show("One of the settings is set.");
}
}
[Flags()]
private enum Settings
{
None = 0,
Setting1 = 1,
Setting2 = 2,
Setting3 = 4,
Setting4 = 8,
All = Setting1 | Setting2 | Setting3 | Setting4
} http://www.csvreader.com
Hi shriop,
I agree that an enum is appropriate, but I'm not sure about "[Flags()]".
MSDN says it should be used, but doesn't say why. I've used bitwise enums
without Flags, and they work fine. Any info?
Cheers,
Javaman.
"shriop" wrote: Just as an added measure, you might want to use a flag enum in your code to make this code section more readable.
private void Form1_Load(object sender, System.EventArgs e) { int someValue = 7; Settings settings = (Settings) someValue; if (settings == Settings.None) { MessageBox.Show("None"); } if ((settings | Settings.Setting1 | Settings.Setting2 | Settings.Setting3 | Settings.Setting4) > 0) { MessageBox.Show("One of the settings is set."); } }
[Flags()] private enum Settings { None = 0, Setting1 = 1, Setting2 = 2, Setting3 = 4, Setting4 = 8, All = Setting1 | Setting2 | Setting3 | Setting4 } http://www.csvreader.com
I'm repeating what a few have said already, but I hope that this sums it up... I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8.
In c/c++ true and false can be used as ints, but in c#, true and false are
of type bool, which is not compatible with int. To get a bool from a number,
you must do a omparison with 0.
eg.
if ((0x11 & 0x01) != 0)
{
Console.WriteLine("True");
}
Note that you should almost always use != 0 to test for true, rather than ==
1 (or == 0x10, or whatever).
C# is so strict about this, that it doesn't even permit a cast from int to
bool. (eg. (bool) 5 won't compile). WTG.
To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe the operator I need to use is &. Can anyone tell me how this is done?
The operators you need are & and |, for the bitwise operations, and == and
!= to get the boolean.
You should use an enum to define your bitwise constants.
This program shows how it's down...note particularly the function
"Eligible", which I discuss below...
class BitwiseExample
{
// Declare an enum with fixed bit settings
enum EmploymentBasis
{
None = 0,
FullTime = 1,
PartTime = 2,
Casual = 4,
Temporary = 8,
}
// Use bitwise operations to test set inclusion.
// Return true if the Employment Basis is FullTime
// or ParTime.
static bool Eligible(EmploymentBasis e)
{
return ((e &
(EmploymentBasis.FullTime |
EmploymentBasis.PartTime))
!= 0);
}
public static void Main()
{
// Use our bitwise test for set inclusion.
EmploymentBasis emp1 = EmploymentBasis.FullTime;
EmploymentBasis emp2 = EmploymentBasis.Casual;
Console.WriteLine(
"emp1 eligible for retirement benefits = " + Eligible(emp1));
Console.WriteLine(
"emp2 eligible for retirement benefits = " + Eligible(emp2));
}
}
Output:
emp1 eligible for retirement benefits = True
emp2 eligible for retirement benefits = False
This function shows how to use the &, | and != operators. We use | to build
up a set (FullTime | PartTime), we & to test for inclusion in the set, and we
use != to get a boolean.
static bool Eligible(EmploymentBasis e)
return ((e &
(EmploymentBasis.FullTime |
EmploymentBasis.PartTime))
!= 0);
I've seen in the help files the [Flags] attribute, which is intended for
bitwise enums, and would be used like this
[Flags]
enum EmploymentBasis
{
}
but it is not clear what it does. My example program worked fine without it.
Cheers,
Javaman
"Christopher Weaver" wrote:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8.
To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe the operator I need to use is &. Can anyone tell me how this is done?
This has all been so very helpful.
Thank you all,
Chris. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Randell D. |
last post by:
Why would one use bitwise operators? I can program in various languages in
some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise
operators before, but never understood why...
|
by: jas_lx |
last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes
fairly simple, as long as one has a fundamental understanding of bits,
bytes and binary.
Having done some Win32...
|
by: Paul E Collins |
last post by:
Suppose I have a few Keys objects:
Keys k1 = Keys.V; // V
Keys k2 = Keys.Control | Keys.V; // Ctrl+V
Keys k3 = Keys.Shift | Keys.J; // Shift+J
I need to determine which of these include the...
|
by: Emilio |
last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
|
by: AMDRIT |
last post by:
Gang,
I always get confused when it comes to 1's and 0's. I would like to perform
a bitwise operation on a value based on checked boxes.
Am I doing this right?
assuming...
|
by: shdwsclan |
last post by:
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my...
|
by: Carramba |
last post by:
Hi!
I now that I can't do straight forward any bitwise operation on float
(double etc..). But I wondering what is the easiest/best way to do this?
I was thinking if I have float x=1.1111 so I can...
|
by: Carl Banks |
last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this
is just talk.)
Not many people are bit-fiddling these days. One of the main uses of bit
fields is flags, but that's not...
|
by: Daniel Gutson |
last post by:
Hi,
I just wanted to share another library for doing type-safe bitwise
operations in C++:
http://bitwise-enum.googlecode.com
I found it useful, so hopefully it'll be for somebody else as well....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |