473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Bitwise Operator ~ Help

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.

Nov 22 '05 #1
3 1915
~ 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

"HLong" wrote:
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.

Nov 22 '05 #2
> 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

Nov 22 '05 #3


HLong wrote:
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
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
9
by: Christopher Weaver | last post by:
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...
5
by: Bill Dee | last post by:
I need help converting a tiny piece of code which uses the bitwise complement operator from C# to VB.NET. In C# I currently have this: long useThis = Myclass.ALLCONSTANTS; long doNotUse =...
3
by: Marc | last post by:
I'm trying to set the first three bits to zero on a byte. For some reason, the compiler is casting the number peculiarly when I use the bitwise complement operator. Here's what I think should...
4
by: John Dann | last post by:
I'm trying to generate a 4-byte unsigned integer in VB2005 that encodes a number of bit-level variables. What I need to do is to be able to compute a value for the uint that is based on specific...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.