- Some problem records
- Represent numbers in binary and decimal systems
- Symbol bits and overflow
-
Operation
- shift left (
- Shift to the right (>>)
- Bitwise OR (|)
- Position and (&)
- The difference between the position is (^)
- Bit negation (~)
- Summarize
- Case - bitwise AND to determine odd and even numbers
-
Case -two numbers according to position or exchange
- Exchange steps:
-
Case - collection operations
- 1. Union
- 2. Intersection
- 3. Difference
- 4. Symmetric Difference
- 5. Subset and super set
- Comparison of bit operations and set operations
- Summarize:
Some problem records
- Binary to decimal conversion;
- Binary carry system;
- Sixteen -in -system inlet system;
- How to express negative numbers and decimals in binary;
- Symbol position, overflow
- Binary original code, anti -code, supplementary code understanding
- The operation of binary number;
- How to represent English characters and symbols of binary;
- How to represent various coding characters;
Represent numbers in binary and decimal systems
Figure 1
Sign bit and overflow
todo
Operation
In the computer, all operations will eventually be turned into binary, and then the bit operation operation will be performed. Almost all computer language supports binary operations.
shift left (<<
)
definition: Move all bits of the binary number to the left to the left, and use the position empty on the right side.0
filling.
describe: The left move operation is equivalent to multiplying the number (2^n), where (n) is the number of movement.
Case:
- Binary number
5
The expression is00000101
。 - After one place in the left move:
00001010
(Right now10
)。 - After the left move 2 bits:
00010100
(Right now20
)。 - formula:
5 << 1 = 10
,5 << 2 = 20
。
Shift to the right (>>
)
definition: Shift all the bits of the binary number to the right by the specified number of digits, and fill the empty bits on the left with the sign bit (positive number complement0
, negative complement1
)。
describe: The right shift operation is equivalent to dividing the number by (2^n), where (n) is the number of bits to shift (rounded down).
Case:
- binary number
10
The expression is00001010
。 - After moved to 1 place right:
00000101
(Right now5
)。 - After shifting right by 2 bits:
00000010
(Right now2
)。 - formula:
10 >> 1 = 5
,10 >> 2 = 2
。
Position or (|
)
definition: Logic or operation to each one of the two binary numbers, as long as one bit is1
, the result bit is1
。
describe: used tosome bitsset to1
。
Case:
- binary number
5
The expression is00000101
。 - Binary number
3
The expression is00000011
。 - Press or operate:
00000111
(Right now7
)。 - formula:
5 | 3 = 7
。
bitwise AND (&
)
definition: Perform logical AND operation on each bit of two binary numbers, only when both bits are1
When the result is1
。
describe: used to extract orKeep some bit。
Case:
- binary number
5
The representation is00000101
。 - Binary number
3
The expression is00000011
。 - Bitwise AND operation:
00000001
(Right now1
)。 - formula:
5 & 3 = 1
。
The difference may (^
)
definition: Perform logical XOR operation on each bit of two binary numbers. When the two bits are different, the result bit is1
At the same time, the result is0
。
describe: Used to flip some bits orCompare the difference between two numbers。
Case:
- binary number
5
The expression is00000101
。 - binary number
3
The expression is00000011
。 - Bitwise XOR operation:
00000110
(Right now6
)。 - formula:
5 ^ 3 = 6
。
bit negation (~
)
definition: A logical counter -operation of each one of the binary number,0
Become1
,1
become0
。
describe: used forFlink。
Case:
- binary number
5
The expression is00000101
。 - Take anti -operation by bit:
11111010
(Right now-6
, in two's complement representation). - formula:
~5 = -6
。
Summarize
-
Shift left (
<<
)andshift right (>>
)Used for quickly multiplying or dividing by a power of 2. -
Position or (
|
)Used to set certain bits to1
。 -
Position and (
&
)Used to extract or retain certain bits. -
Bit XOR (
^
)Used to flip certain bit or comparison. -
bit negation (
~
)Used to flip all bits.
These bit operations are widely used in low-level programming, encryption algorithms, image processing, etc., and need to be kept in mind.
Case -according to position and judgment strange number
Just 2 steps:
- Convert numbers to binary numbers;
- The last one is 0 is the even, otherwise it will be a wonderful number.
Case - Bitwise XOR exchange of two numbers
Using the nature of the bitwise XOR operation, the values of two variables A and B can be exchanged through the following steps without using temporary variables to save resources.
Step step:
Example demonstration:
Suppose A = 5, B = 3.
-
initial value:
- (A = 5) (binary:
0101
) - (B = 3) (binary:
0011
)
- (A = 5) (binary:
-
Step one: (A = A \oplus B)
- (A = 5 \oplus 3)
- Binary calculation:
A: 0 1 0 1 B: 0 0 1 1 ------------ A ^ b: 0 1 1 0 (binary `0110`, decimal 6)
- Now (a = 6), (b = 3).
- A now stores the differences between A and B.
-
Step 2: (B = A \ Oplus B)
- (B = 6 \oplus 3)
- Binary calculation:
A: 0 1 1 0 B: 0 0 1 1 ---------- A ^ B: 0 1 0 1 (binary `0101`, decimal 5)
- Now (a = 6), (b = 5).
- B is now restored to the original A.
-
Step 3: (A = A \oplus B)
- (A = 6 \oplus 5)
- Binary calculation:
A: 0 1 1 0 B: 0 1 0 1 ------------ A ^ b: 0 0 1 1 (binary `0011`, decimal 3)
- Now (A = 3), (B = 5).
- A now reverts to its original B.
Case - collection operations
Set operations are used to process a set of unique elements. Common set operations include union, intersection, difference and symmetric difference.
1. Union
- symbol:
|
orunion()
- Rules: Contains all elements in two sets.
- Example:
A = {1, 2, 3} B = {3, 4, 5} result = A | B # {1, 2, 3, 4, 5}
2. Intersection
- symbol:
&
orintersection()
- Rules: Only two elements of the CCP.
- Example:
A = {1, 3, 8} B = {4, 8} result = A & B # {8}
Step demonstration:
Suppose we give the numbers 1 to 8 a number 1-8,
If a number is in the set, the corresponding position is 1, otherwise it is 0,
Then the set A =10000101
,
Eb each = =10001000
result = A & B
result = {8}
3. Difference
- symbol:
-
ordifference()
- Rule: Elements contained in the first set but not in the second set.
- Example:
A = {1, 2, 3} B = {3, 4, 5} result = A - B # {1, 2}
4. Symmetric Difference
- symbol:
^
orsymmetric_difference()
- Rule: Include elements that are not co-occurring in both sets.
- Example:
A = {1, 2, 3} B = {3, 4, 5} result = A ^ B # {1, 2, 4, 5}
5. Subset and super set
- Sub -symbol:
<=
orissubset()
- Super set symbol:
>=
orissuperset()
- Rule: Determine whether a set is a subset or superset of another set.
- Example:
A = {1, 2} B = {1, 2, 3} is_subset = A <= B # True is_superset = B >= A # True
Comparison between bit operation and collection operation
Type | Bit operation (binary) | Set operations |
---|---|---|
and | & |
& |
or | ` | ` |
XOR | ^ |
^ |
negate | ~ |
No direct corresponding operation |
Left move/right move |
<< / >>
|
No direct corresponding operation |
Summarize:
-
Computers operate the binary position directly through the bit operation, which is suitable for underlying programming and performance optimization. The above cases also reflect their efficiency;
-
Operation according to position is the most saving resources;
-
Collection operations are suitable for data filtering;
-
Bit operations and set operations have certain similarities in symbols, but their application scenarios are different. It can only be said that collections are based on bit operations.