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

  • Thread starter ionlylooklazy
  • Start date
In summary, There are two ways to perform 2's complement on a byte - either by using a built-in function or manually converting the byte to binary and performing the necessary operations. In C#, the bitwise NOT operator can be used, while in VB.Net the Not operator can be used. Both methods involve converting the byte to an integer and adding 1 after performing the bitwise NOT.
  • #1
ionlylooklazy
30
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
  • #2
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:
  • #3
thank you very much again!
 

1. What is the purpose of using 2's complement on a byte?

The purpose of using 2's complement on a byte is to represent negative numbers in binary form. This is a method commonly used in computer systems to perform arithmetic operations on both positive and negative numbers.

2. How does the 2's complement function work?

The 2's complement function works by flipping all the bits in a binary number and then adding 1 to the result. This creates the equivalent negative number in binary form. For example, the 2's complement of 1010 is 0101+1 = 0110, which represents -6 in decimal form.

3. Can you convert any binary number to its 2's complement?

Yes, you can convert any binary number to its 2's complement by using the method described above. However, the resulting number may be larger than the original number due to the addition of the extra 1.

4. How do you perform binary conversion using 2's complement?

To perform binary conversion using 2's complement, you first convert the decimal number to its binary form. Then, if the number is negative, you take the 2's complement of the binary number. Finally, you can convert the resulting binary number back to decimal form.

5. What are some advantages of using 2's complement over other methods of representing negative numbers?

One advantage of using 2's complement is that it is a simple and efficient method for representing negative numbers in binary form. It also allows for easy addition and subtraction of both positive and negative numbers using the same binary operations. Additionally, it avoids the need for a separate sign bit, which can save memory space in computer systems.

Similar threads

  • Programming and Computer Science
Replies
5
Views
758
Replies
1
Views
584
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
29
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
624
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
982
Back
Top