Solved

How to set a bit in a short value

Posted on 2010-11-08
5
293 Views
Last Modified: 2012-06-22
I have a VB.NET windows forms project. One of the values i am sending to a tool is a short value with a max of 2050. I need to be able to set the last bit of this short to either a 0 or a 1 based on some other criteria.

Need suggestions and / or pointers to how to do bit manipulation in VB.NET. Found lots of bit shifting but thats not what i need.

Cheers
0
Comment
Question by:Halon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34087133
You can use the standard boolean operators (AND, OR, etc.) to do bit manipulation:
    Sub Main()
        Dim test As Short = 0
        Dim mask As Short = 1 << 2

        test = test Or mask

        Console.WriteLine(test)
        Console.ReadKey()
    End Sub

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34087157
I meant to say "logic", not "boolean"  :)
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 500 total points
ID: 34087222
Dim s As Short = 100

'Set the bit
s = CType(s Or 1, Short)

Debug.WriteLine(s)

'Clear the bit
s = CType(s And 254, Short)

Debug.WriteLine(s)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34087276
Hold on.
'Clear the bit (if positive)
s = CType(s And 32766, Short)
0
 

Author Closing Comment

by:Halon
ID: 34102957
Worked great, cheers!
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
YouTube API get the Video Tags - vb.net 7 51
Broken .resx file generating errors 18 43
Set arraylist as datatable 2 29
Datagridview column resizing 8 26
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question