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. 3 1914
~ 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.
> 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 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 <=- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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 =...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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: 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: 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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |