Solved

How to set a bit in a short value

Posted on 2010-11-08
5
292 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

762 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