VB: 2's Complement on Byte - Function & Binary Conversion

  • Thread starter Thread starter ionlylooklazy
  • Start date Start date
Click For Summary
To perform a 2's complement operation on a byte, the process involves converting the byte or character to an integer, applying a bitwise NOT operation, and then adding 1. In C#, this can be achieved using the bitwise NOT operator (~) followed by addition. For example, if the byte value is 230, the operation would be implemented as `int bAfter2sC = ~b + 1`. Similarly, in VB.Net, the operation can be executed with `bAfter2sC = Not(b) + 1`. This method effectively allows for the conversion of a byte to its 2's complement representation.
ionlylooklazy
Messages
30
Reaction score
0
is there a function available that will perform 2's complement on a byte?
or a way to convert a byte or char to binary so I can do the operation myself?

thanks,
ioll
 
Technology news on Phys.org
It should be pretty simple. All you need to do is perform a bitwise NOT and then add a 1. You would convert your byte or char to an Int, perform the bitwise Not, and then add a 1. In C# the ~ is the bitwise NOT, so you would do:
Code:
int b = 230;
int bAfter2sC = ~b + 1

In VB.Net try:
Code:
Dim b As Integer
Dim bAfter2sC As Integer
bAfter2sC = Not(b) + 1
 
Last edited:
thank you very much again!
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
5
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 29 ·
Replies
29
Views
3K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K