Into the system
- Binary and decimal convert to each other
- Decimal and hexadecimal conversions
Source code, inverse code, complement
Source code, inverse code and complement: in computers, signed numbers are represented in three ways: source code, inverse code and complement. All data operations are carried out using complement codes. Note: the source, inverse and complement of positive numbers are the same. Negative numbers are a slightly more complex source code: the binary fixed-point representation, the highest bit is a sign bit, "0" for positive, "1" for negative. Inverse: The inverse of a negative number is the reverse of its source code bit by bit, except in the sign bit (which is the highest bit). Complement: The complement of a negative number is added to the end of its inverse by 1. Note: when finding the inverse, the highest bit is the sign bit. The sign bit of a positive number is 0 and that of a negative number is 1Copy the code
Positive 5: 5 binary number: 00000101 source code, complement code, inverse code: 00000101 negative -5; Binary number of 5:00000101 Source code: 10000101 Reverse code: 111111010 Complement code: 111111011Copy the code
Shift operation
Shift operation: <<: moves the whole binary of the operand to the left by the specified number of digits, and replaces the empty space with "0". >>: Moves the whole binary of the operand to the right by the specified number of digits, and replaces the empty space with "sign bit" if the positive number is replaced with "0" if the negative number is replaced with "1"Copy the code
/* * 2 binary: * 00000000_00000000_00000000_00000000_00000010 * left 3 bits: * 00000_00000000_00000000_00000000_00000010000 // Decimal: 0 * 2^0 +... + 1 * 2^4 = 16 * move 3 bits * 00000000_00000000_00000000_00000010 // Decimal: 1 * 2^1 = 2 */ system.out. println(2 << 3); // 16 System.out.println(16 >> 3); // 2 /* * -16 The highest bit of the binary number is supplemented by 1 (the highest bit of the negative number is 1) * Source code: 10000_00000000_00000000_00000010000 * Reverse code: 11111 _11111111_11111111_11111101111 / / negative radix-minus-one complement highest constant * complement: 11111_11111111_11111111_11111110000 // The complement of a negative number is moved 3 bits to the right at the end of its inverse by 1 * (move 3 bits to the right based on the complement. The space after the right shift is filled with "sign bits". * 11111111_11111111_11111111_11111110 */ System.out.println(-16 >> 3); / / - 2Copy the code