What is a bit operation?

All the numbers in the program are stored in binary form in computer memory. Bitwise operations operate directly on the binary bits of an integer in memory.

Here’s a brief explanation of bitwise operations from Wikipedia:

In digital computer programming, a bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. It is a fast, simple action directly supported by the processor, and is used to manipulate values for comparisons and calculations. On simple low-cost processors, typically, bitwise operations are substantially faster than division, several times faster than multiplication, and sometimes significantly faster than addition. While modern processors usually perform addition and multiplication just as fast as bitwise operations due to their longer instruction pipelines and other architectural design choices, bitwise operations do commonly use less power because of the reduced use of resources.

In digital arithmetic programming, a bit operation operates on one or more bit combinations or binary digits at the level of a single bit.

Bitwise operations run directly on the processor, making them very simple and fast, and are often used to compare or calculate data.

On simple, low-cost processors, bitwise operations are much faster than division, and in some cases even faster than multiplication and addition.

But on modern processors, thanks to a longer instruction pipeline and other structural design choices, addition and multiplication can be just as fast as bitwise operations, but bitwise operations usually consume less energy because they are more resource-efficient.

A logical operator for bitwise operations

  1. With operation &

The essence of bit and operation is to carry out logical and operation bit by bit according to the corresponding binary number of the two data involved in the operation.

For negative numbers, operate on their complement.

  1. | or operation

The essence of bit or operation is to carry out logic or operation bit by bit according to the corresponding binary number of the two data involved in operation.

Logical operators | | a difference is: the or operator | conditions “or” operator (| |) perform Boolean logic “or” of operands, but only when necessary to calculate the second operand.

X | | y | x, y if x is true, do not calculate y (because no matter why y values, “or” the result of the operation to true). This is called a “short circuit” calculation.

  1. ^ an exclusive or

The essence of bit-xOR operation is to perform logical XOR operation on the two data bits according to the corresponding binary number. The result of the corresponding bit is true only if the binary numbers of the corresponding bit are mutually exclusive.

  1. ~ a non

The essence of bit-non-operation is to carry out logical non-operation bit by bit between the two data involved in the operation according to the corresponding binary number.

Examples of application

For example, there is a company with three executives a, B and C (from the highest level to the lowest level). For the products sold by the company, the executives can provide quotations, but not every executive is required to provide quotations. Therefore, there are seven situations: A, B, C, AB, AC, BC and ABC. Require a record of each supervisor who provided a quote for a product, and the final valid quote is the highest of them.

Now design the price table, in which the fields AP, BP and CP respectively represent the quotation provided by each supervisor; Field P represents all supervisors who have provided quotes, where A is 1, B is 2, and C is 4, and the p field stores the sum of these three numbers.

For example, if supervisor B offers offer 100 and Supervisor C offers offer 93, ap=null, BP =100, CP =93,p=6(that is, 2+4), where the valid offer is B’s offer 100.

To determine whether a supervisor’s quotation is included, you can use and calculate:

1 converts to binary 0001. 2 Converts to binary 0010. 4 Converts to binary 0100

When p is 6:6 is converted to binary 0110

6&1, namely 0110&0001=0000=0, indicates that the quotation of supervisor A is not included.

6&2, i.e. 0110&0010=0010>0, represents the offer containing supervisor B.

According to whether is greater than 0, can determine whether to include a supervisor’s quotation;

Below is SQL for creating tables, inserting data, and querying with bitwise operations.

CREATE TABLE price
(
 id INT PRIMARY KEY,
 ap INT DEFAULT NULL,
 bp INT DEFAULT NULL,
 cp INT DEFAULT NULL,
 p INT DEFAULT NULL
);

INSERT INTO price VALUES
(1,NULL,100,93,6),
(2,188,170,203,7),
(3,NULL,14,NULL,2);

INSERT INTO price VALUES
(4,NULL,NULL,NULL,NULL),
(5,44,55,66,7),
(6,NULL,NULL,430,4);


SELECT
id 'Product Number',
CASE WHEN p&ap>0 THEN ap
     WHEN p&bp>0 THEN bp
     WHEN p&cp>0 THEN cp
     ELSE 'There is no valid quote for this product'
     END 'Valid offer',
CASE WHEN p&ap>0 THEN 'ap'
     WHEN p&bp>0 THEN 'bp'
     WHEN p&cp>0 THEN 'cp'
     ELSE 'no'
     END 'Quotation Supervisor'
FROM price;

Copy the code