Bit Operations (Java Binary Operations)

bit operation

Bitwise Operations are operations that operate on binary bits. They are very common in computer science and computer programming for bit-level manipulation and manipulation of binary data.

type

Bitwise operations are primarily used to manipulate integer types, since they are bit-based operations. This includes integer types (such as integers and unsigned integers) and character types (such as ASCII characters).

  1. Signed integer types: eg int、long、short、byteetc. Integers of these types can represent positive numbers, negative numbers, and zero, using two's complement representation.

  2. Unsigned integer types: eg unsigned int、unsigned long、unsigned short. Integers of these types can only represent non-negative numbers and zero, do not use a sign bit, and are usually represented in pure binary.

  3. Byte type: eg char. Byte types are usually used to represent characters, but they can also be used as integer types for bit operations.

However, some programming languages ​​also allow bit operations on other data types, such as Boolean types ( bool) or bit-set types. In these cases, bitwise operations are often used to manipulate individual bit flags or bit sets.

For non-integer types, the results of bitwise operations may be affected by data type restrictions and language specifications.

Advantage

The bit operation operation is the underlying operation supported by the processor, and the underlying hardware only supports numbers such as 01, so the bit operation runs very fast.
Although modern computer processors have longer instruction pipelines and better architectural designs, addition and multiplication operations are almost as fast as bit operations, but bit operations consume fewer resources.

operator

The following are common bitwise operators:

  1. AND operation ( AND): "&"Indicated by the symbol , the logical AND operation is performed on the corresponding bits of the two operands. Only when the corresponding bits of the two operands are both 1, the result bit is 1, otherwise it is 0.
    ( 1&1=1,1&0=0,0&0=0)

  2. Or operation ( OR): "|"expressed by the symbol , the logical OR operation is performed on the corresponding bits of the two operands. As long as one of the corresponding bits of the two operands is 1, the result bit is 1, otherwise it is 0.
    ( 1|1=1 ,1|0=1, 0|0=0)

  3. Exclusive OR operation ( XOR): "^"Indicated by the symbol , the exclusive OR operation is performed on the corresponding bits of the two operands. Only when the corresponding bits of the two operands are different, the result bit is 1, otherwise it is 0.
    ( 1^1=0, 1^0=1, 0^0=0)

  4. Not operation ( NOT): "~"Indicated by a symbol, each bit of the operand is inverted, that is, 1 is changed to 0, and 0 is changed to 1.
    ( ~1=0, ~0=1)

  5. Left shift operation ( Left Shift): "<<"Indicated by the symbol , shift the binary bits of an operand to the left by the specified number of bits, and fill the vacant bits on the right with 0.

  6. Right shift operation ( Right Shift): ">>"Indicated by a sign, the binary bit of an operand is shifted to the right by the specified number of bits, and the vacant bit on the left is filled with the original sign bit.
    When performing a right shift operation, if the number involved in the operation is a positive number, add 0 to the high bit; if it is a negative number, add 1 to the high bit.

  7. Unsigned right shift: An unsigned right shift is usually ">>>"expressed using the operator. Unsigned right shift is a bit operation that shifts the binary representation of an integer to the right by a specified number of bits, filling the vacated bits on the right with 0. It simply shifts the bits out to the right of the integer regardless of the sign bit.

These bitwise operators can be used to perform bit-level mask operations, extraction and setting of bit operands, processing of flag bits, etc. They are frequently used in some low-level computer operations, bit-field manipulation, and optimization algorithms.

Effects of Left Shift (Shift Right) Operators on Data

The left shift operator ( <<) is used to shift the binary representation of a number to the left by the specified number of bits, and fill the vacated bits on the right with 0. The left shift operation can expand the value, and its effect is equivalent to multiplying the value by the shift power of 2.

The left shift operator affects the data as follows:

  1. Shifting a number to the left by n bits is equivalent to multiplying the number by 2 to the nth power.

  2. A left shift operation changes the binary representation of the original value by the number of bits shifted to the left causing the vacated bits on the right to be filled with zeros.

  3. Left shift operations may cause numeric overflow. If the left-shifted result exceeds the representation range of the data type, the high-order part will be lost, and only the low-order effective value will be retained.

Here is an example showing how to use the left shift operator to left shift a number:

int num = 42;
int shiftAmount = 2;

int result = num << shiftAmount;
System.out.println(result);

In the example above, the initial value numis 42, and shifting it to the left by 2 bits results in 168. <<Shifting the value 42 to the left by 2 bits through the left shift operator is equivalent to multiplying the value by 2 to the power of 2, that is, 42 * (2^2) = 168 .

It should be noted that the number of shifts by the left shift operator cannot exceed the number of bits of the data type. In some programming languages, shifting more than the number of bits in a data type is undefined behavior and may lead to unexpected results or errors. Therefore, when performing a left shift operation, it is necessary to ensure that the number of shifts is within the legal range.

Left shift is multiplied by 2 to the nth power, and right shift is divided by 2 to the nth power.

Int binary

intThe type is an integer type, usually represented by 32-bit binary. In most programming languages, inttypes use two's complement notation, where the highest bit is the sign bit (0 for positive numbers, 1 for negative numbers), and the remaining 31 bits are used to represent the numeric portion.

Int 2 converted to binary:

00000000 00000000 00000000 00000010

Two's complement notation range:

 -2^31 ~ 2^31-110000000 00000000 00000000 00000000 
 	~ 01111111 11111111 11111111 11111111)

Note: programming languages ​​usually provide conversion functions or methods, such as Integer.toBinaryString() in Java, which can conveniently convert int type to binary string for output.

More

https://blog.csdn.net/qq_42265220/article/details/118386893

Guess you like

Origin blog.csdn.net/weixin_35691921/article/details/105341039