Home>Article>Backend Development> What are the 1's complement and 2's complement of binary numbers?

What are the 1's complement and 2's complement of binary numbers?

WBOY
WBOY forward
2023-09-11 23:33:02 1595browse

What are the 1s complement and 2s complement of binary numbers?

Binary numbers are represented in base 2. It only uses the two digits "0" and "1". Each digit in a binary number is abit.

Example binary number - 0100010111

1's complement number

The binary number's complement number is by reversing the digits of the binary number, that is, 1 is converted to 0, and 0 is converted is obtained as 1.

Example

1’s Complement of 101100 = 010011

2's complement

The complement of a binary number is the complement of the binary number plus 1, that is, 1's complement of 1.

Example

2’s complement of 101101 is 010011.

Sample code

Code to find one and two's complement -

#include  #include using namespace std; int main() { char binary[10] = "01001011"; cout<<“Binary number is ”<=0; i--) { // cout<
      

Output

Binary number is 01001011 One’s complement is 10110100 Two’s complement is 10110101

The above is the detailed content of What are the 1's complement and 2's complement of binary numbers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete