bitwise-AND

  • Thread starter Thread starter Shawn B.
  • Start date Start date
S

Shawn B.

Greetings,

If I havea a numeric value, lets say, "uint value" and I want to set the 5th
bit, I can't just say "value = (value & 0x20) because it yeilds a boolean
evalutation. I can't just add ((2^5)-1) because I am not guaranteed it will
set the 5th bit.


Thanks,
Shawn
 
Shawn B. said:
If I havea a numeric value, lets say, "uint value" and I want to set the 5th
bit, I can't just say "value = (value & 0x20) because it yeilds a boolean
evalutation. I can't just add ((2^5)-1) because I am not guaranteed it will
set the 5th bit.

To set the bit you would say

value = value | 0x20;
or
value |= 0x20;

-- Alan
 
Shawn B. said:
If I havea a numeric value, lets say, "uint value" and I want to set the 5th
bit, I can't just say "value = (value & 0x20) because it yeilds a boolean
evalutation.

No it doesn't. && yields a boolean result, but & doesn't. Here's an
example:

using System;

class Test
{
static void Main()
{
uint x = 21;
uint y = 5;

uint z = x&y;

Console.WriteLine (z);
}
}
 
Does the reverse work for clearing the bit? (perhaps I should try before
asking =))


Thanks,
Shawn
 
Shawn B. said:
Does the reverse work for clearing the bit? (perhaps I should try before
asking =))

As long as you AND it with the comlementary bits yes (so 11101111 = 0xEF)
or XOR it with the original value if you are SURE it's set. All bitwise
operators can be found in most .NET / C# documentation I presume
 
Does the reverse work for clearing the bit? (perhaps I should try before
asking =))

Not exactly. To clear the bit you AND with the NOT'd mask.

Like this:

value = value & ~0x20;

(Does C# have ~?)

If you look at it in binary, like the computer does, it looks like this:

Say value = 128 = 1000 0000
0x20 = 32 = 0010 0000
----------
Or'ing yields 1010 0000 = 160 (0xA0)

Now to clear the bit:

value = 160 = 1010 0000
~0x20 = 223 = 1101 1111
 
Shawn B. said:
Does the reverse work for clearing the bit? (perhaps I should try before
asking =))

OR to set, AND with complement to clear, and XOR to flip it.

|, &, ^ are the bitwise operators for this.

||, && are the boolean operators.

-- Alan
 
Hi Shawn,
Shawn B. said:
Does the reverse work for clearing the bit? (perhaps I should try before
asking =))
1. If you want to set a bit you has to use bitwise OR operation as Alan
suggest. so if you want to *set* the fifth ( or sixth if we count them
starting from one ;) ) bit you should follow Alan's suggestion
value = value | 0x20;
or
value |= 0x20;


2. To clear a bit you should use

value = value & ~0x20;
or
value &= ~0x20;

3. If you want to check if the fifth bit is set then you can use

bool isBitSet = (value & 0x20) != 0

B\rgds
100
 
How do you look at it in binary ?

100 said:
Hi Shawn,

1. If you want to set a bit you has to use bitwise OR operation as Alan
suggest. so if you want to *set* the fifth ( or sixth if we count them
starting from one ;) ) bit you should follow Alan's suggestion
value = value | 0x20;
or
value |= 0x20;


2. To clear a bit you should use

value = value & ~0x20;
or
value &= ~0x20;

3. If you want to check if the fifth bit is set then you can use

bool isBitSet = (value & 0x20) != 0

B\rgds
100
 
Back
Top