Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to set a bit in a short value

Posted on 2010-11-08
5
Medium Priority
?
299 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:Winston Smith
[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 2000 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:Winston Smith
ID: 34102957
Worked great, cheers!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

604 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