Solved

How to set a bit in a short value

Posted on 2010-11-08
5
288 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
  • 2
  • 2
5 Comments
 
LVL 74

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 74

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now