Bitwise Operator ~ Help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to understand an example where the Least Significant Bit is
replaced. The code is in C# and I am having problems with a line that reads:
B= (byte) (Value == 1 ? B | (1 << poss) : B & ~ (1 << poss));
here B is the byte where we change the bit (Value) and poss = 0
I think in VB it would be something like:

if (Value = 1) then
B=B or (1 << poss)
else
B= B and [here I don't know what to do with ~] (1 << poss)
endif

I think that the condition is: if value is equal to 1 then B states the same
or it would be (1 << poss), I don't understand this since shifting 1 to 0 is
the same as 1. The else condition I don't understand at all. Could someone
please help me understand this, I would really appreciate the help.
 
~ is equivalent to 'Not' in VB (VB uses Not for both logical and bitwise
negation).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter
 
I am trying to understand an example where the Least Significant Bit is
replaced. The code is in C# and I am having problems with a line that reads:
B= (byte) (Value == 1 ? B | (1 << poss) : B & ~ (1 << poss));
here B is the byte where we change the bit (Value) and poss = 0
I think in VB it would be something like:

if (Value = 1) then
B=B or (1 << poss)
else
B= B and [here I don't know what to do with ~] (1 << poss)
endif

I think that the condition is: if value is equal to 1 then B states the same
or it would be (1 << poss), I don't understand this since shifting 1 to 0 is
the same as 1. The else condition I don't understand at all. Could someone
please help me understand this, I would really appreciate the help.

As you have coded it, change the line
B= B and [here I don't know what to do with ~] (1 << poss)
to
B= B and (&hFF xor (1 << poss))


alternatively, the entire expression is

k=1<<poss
b=b or k
if value<>1 then b=b xor k
 
HLong said:
I am trying to understand an example where the Least Significant Bit is
replaced. The code is in C# and I am having problems with a line that reads:
B= (byte) (Value == 1 ? B | (1 << poss) : B & ~ (1 << poss));
here B is the byte where we change the bit (Value) and poss = 0

So this can be formulated as a function:

void SetBit(ref B, int poss, int Value) {
B = (byte)(Value == 1 ? B | (1 << poss : B & ~(1 << poss);
}

I'd like Value to be a boolean, that way there is no room for the
"interpretation-error" in the above code if Value > 1 or < 0.

void SetBit(ref B, int poss, bool Value) {
B = (byte)(Value ? B | (1 << poss : B & ~(1 << poss);
}

Rewrite the code to make it clear what the "?" operator is used for:

void SetBit(ref B, int poss, bool Value) {
byte x;
if ( Value )
x = (byte)(B | (1 << poss));
else
x = (byte)(B & ~(1 << poss));
B = x;
}

So, if the bit at poss is to be set, a 1 is shifted poss bits and or'ed
onto B. This clearly set's bit poss, and touches no other bit.

If the bit is to be cleared, B is and'ed with the bitwise invertion of 1
shifted poss bits (so it's a pattern of 1's, except in bit poss, where
it's a zero). This clearly removes any set bit poss, and leaves all
other bits intact.
I think that the condition is: if value is equal to 1 then B states the same
or it would be (1 << poss), I don't understand this since shifting 1 to 0 is
the same as 1. The else condition I don't understand at all. Could someone
please help me understand this, I would really appreciate the help.

Here is the way I think most clearly expresses what is (or should be :)
going on:

static void SetBit(ref byte B, int poss, bool Value)
{
if ( Value )
B |= (byte)(1 << poss); // or the bit onto B
else
B &= (byte)~(1 << poss); // mask the bit out of B
}
 
Back
Top