Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / October 2005

Tip: Looking for answers? Try searching our database.

Bitwise Operator ~ Help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
HLong - 29 Oct 2005 20:46 GMT
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.
David Anton - 29 Oct 2005 21:55 GMT
~ is equivalent to 'Not' in VB (VB uses Not for both logical and bitwise
negation).
Signature

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:
[quoted text clipped - 12 lines]
> 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.
AMercer - 29 Oct 2005 21:55 GMT
> 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:
[quoted text clipped - 12 lines]
> 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
Helge Jensen - 30 Oct 2005 11:12 GMT
> 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
   }

Signature

Helge Jensen
 mailto:helge.jensen@slog.dk
 sip:helge.jensen@slog.dk
              -=> Sebastian cover-music: http://ungdomshus.nu <=-


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.