We help IT Professionals succeed at work.

How to set a bit in a short value

Winston Smith
on
323 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
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
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

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
I meant to say "logic", not "boolean"  :)
Top Expert 2005
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2005

Commented:
Hold on.
'Clear the bit (if positive)
s = CType(s And 32766, Short)
Winston SmithDeveloper

Author

Commented:
Worked great, cheers!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.