Bit operations are the basis of a program, but to be clear, you can do anything without knowing it. However, by knowing it, you can expand the choices of programs, and there are some situations where it is convenient to actually use it. In addition, bit operations can be used in many computer languages, so once you learn them, you can use them in various environments. In this article, for those who do not know bit operations, we will explain a simple example of what bit operations are.

A computer is a collection of innumerable ON / OFF switches. This switch is called a bit, and one switch is called a bit. And this switch is operated as a set of eight, and these eight groups are called bytes, and one set of groups is one byte. You may have heard that a program is a collection of 0s and 1s, but it comes from here. When notation, it is written as 00000000, and it counts as 0th bit, 1st bit, etc. from the right side.

- Note that it is often counted from 1

Bit operations control the ON / OFF switching of innumerable switches. It's just a word of ON / OFF, but multiple commands are prepared for various situations.

And for ON / OFF control, basically multiples of 8 are performed together. That is, in bytes. There are various types such as 1-byte variables and 2-byte variables in each language, but all the bytes of that variable are collectively issued as an instruction.

Operators are calculation symbols used when calculating + and-. Bit operations are special calculation symbols that are not used in mathematics, and there are six. Basically, the returned results are checked in the order of the 1st bit and the 2nd bit, and the result is entered in the 1st and 2nd bits, respectively.

&（AND） Compares the two switches and returns 1 if both are 1, and 0 if either one is 0. It is used to check whether the flag is set when the flag is bit-managed. Example: c = a & b a 11110000 b 10101010 ↓ c 10100000

|（OR） Compares two switches and returns 1 if either is 1 or 0 if both are 0. It is used when setting a flag when bit management is performed. Example: c = a | b a 11110000 b 10101010 ↓ c 11111010

Reverses all switch ON / OFF states, changing 0 to 1 and 1 to 0. Use this when you want to make the opposite state. Example: c =! a a 11110000 ↓ c 00001111

^（XOR） Compares two switches and returns 1 if only one of them is 1, 0 if both are 1 or 0 if both are 1. It is used when you want to find out what is different. Example: c = a ^ b a 11110000 b 10101010 ↓ c 01011010

Shift the ON / OFF status of the switch by one to the right. 0 will be entered in the vacant frame due to the shift, and the part that exceeds the fixed number of digits will disappear. If it sticks out, it disappears, which is called overflow. It is used when manipulating bits using a loop. Note: c = a >> 1 a 11110000 ↓ c 01111000

Shift the ON / OFF status of the switch by one to the left. Except for this part, it is almost the same as the right bit shift. Note: c = a >> 1 a 11110000 ↓ c 11100000

The above is the basis of bit operation. By combining these, it is possible to perform computer-specific calculations that cannot be done simply by +-× ÷. When you actually start using it, you often care about how many bytes the variable is, but it is worthwhile to use overflow intentionally. The world is enthusiastic about Bitcoin, but please remember the bit operation once in a while!

Recommended Posts