Converting a byte to binary and performing 2's complement

  • Thread starter Thread starter ionlylooklazy
  • Start date Start date
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
2 replies · 20K views
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
 
Physics 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: