Bitwise operator - Performs bitwise operations
Bit operation is an operation to obtain the logical sum and logical product of each bit. There are few opportunities to use bit operations, but when using functions that directly use C language libraries such as sysopen, it is possible to perform bit operations. I have. It may also be used when you want to perform low-level data manipulation on a bit-by-bit basis.
Bit operations include AND, OR, Exclusive OR, and negation.
operator | meaning | Bitwise operation example | Bitwise operation result |
& | Logical AND | 1100 & 1010 | 1000 |
| | Logical sum | 1100 | 1010 | 1110 |
^^ | Exclusive OR | 1100 ^ 1010 | 0110 |
~ | denial | ^ 1100 | 0011 |
Binary, hexadecimal, and decimal support
Binary number | Decimal number |
0001 | 01 01 |
0010 | 02 02 |
0011 | 03 03 |
0100 | 04 |
0101 | 05 05 |
0110 | 06 06 |
0111 | 07 07 |
1000 | 08 08 |
1001 | 09 09 |
1010 | Ten |
1011 | 11 |
1100 | 12 |
1101 | 13 |
1110 | 14 |
1111 | 15 |
Binary carry and decimal
Binary | |
00000001 | 1 |
00000010 | 2 |
00000100 | Four |
00001000 | 8 |
00010000 | 16 |
00100000 | 32 |
01000000 | 64 |
10000000 | 128 |
It corresponds to doubling in decimal and raising one place in binary.
Bit shift operator
Bit shift is to shift the arrangement of bits to the left or right. There are two types of bit shifts: right bit shift and left bit shift. From a mathematical point of view, the left bit shift is double and the right bit shift is the same as his quotient divided by two.
operator | meaning |
>> >> | Right bit shift |
<< | Left bit shift |
Bit shift operator
# Left bit shift $num << 1; $num << 2; $num << 3; # Right bit shift $num >> 1; $num >> 2; $num >> 3;<< and >> are bit shift operators. The shift operation is performed only for the specified numerical value.
What is the above binary number in decimal? It can be calculated from the place and bit values as follows.
Rank | 7 | 6 | Five |
bit | 0 | 0 | 1 |
2 to the 7th power x 0 + 2 to the 6th power x 0 + 2 to the 5th power x 1 + 2 to the 4th power x 0 + 2 to the 3rd power x 0 + 2 squared x 1 + 2 to the 1st power x 0 + 2 to the 0th power x 0
That is,
128 x 0 + 64 x 0 + 32 x 1 + 16 x 0 + 8 x 0 + 4 x 1 + 2 x 0 + 1 x 0 + = 32 + 4 = 36
Meaning of left bit shift
Left bit shift is equivalent to doubling the value.
Rank | 7 | 6 | Five |
bit |
0 | 0 | 1 |
If you shift the left bit
Rank | 7 | 6 | Five |
bit | 0 | 1 | 0 |
When calculated
128 x 0 + 64 x 1 + 32 x 0 + 16 x 0 + 8 x 1 + 4 x 0 + 2 x 0 + 1 x 0 + = 64 + 8 = 72
It certainly doubles.
On the contrary, right bit shift is the same as the quotient divided by 2.
Understand the meaning of bits in a computer
If you don't understand the meaning of a bit, please click here.
A bit is the smallest unit of data that a computer handles. There are only two bit states, "standing" and "not standing". Expressed in binary, it means "1" or "0". A sequence of 1s and 0s is called a bit string.
Some computer devices are called memory, which is a device that stores a huge string of bits.
The parable of a light bulb
I think it's difficult to imagine memory at first, but it's easy to imagine if you think of a lot of light bulbs lined up in a straight line. There are many light bulbs lined up, and there are "light bulbs with" and "light bulbs without".
○ × ○ × ○ ○ ○ ×
As mentioned above, suppose you have 8 light bulbs. You can express "on" and "not on" with one light bulb. In other words, one light bulb can be used in two ways. If there are eight, 2 to the 8th power can express 256 ways.
Information transmission using light bulbs
I will try to convey information using a light bulb. Let's say that Mr. A and Mr. B have made the following arrangements regarding how to light a light bulb.
○○○○ ×××× → Please help me. × ○ ○ ○ ○ × × × → I arrived safely.
In this way, Mr. A and Mr. B can exchange information using light bulbs even in places where nothing can be seen at night or where no sound can be heard in the distance. Information can be exchanged by human beings giving meaning to symbols that are meaningless by themselves, such as "○○○○ ××××".
Gives the mathematical meaning of a sequence of light bulbs in binary
It gives the mathematical meaning of a binary number as well as a light bulb.
×××××××× → 00000000 ××××××× ○ → 00000001 ○ × × × × ○ × ○ → 10000101
By doing this, the mere arrangement of light bulbs has a mathematical meaning. This is called a bit string and is the basis of computer calculations.
Example code
The following is an example of bit operation and an example of expressing a flag with bits.
use strict; use warnings; # You can express it in binary by prefixing the number with 0b. # Fill in 4 digits left 0 with the format specification %04b # You can output binary representation. print "1: Logical product of binary numbers 1100 and 1010\n"; printf("%04b", 0b1100 & 0b1010); print "\n"; print "2: OR of binary numbers 1100 and 1010\n"; printf("%04b", 0b1100 | 0b1010); print "\n"; print "3: Exclusive OR of binary numbers 1100 and 1010\n"; printf("%04b", 0b1100 ^ 0b1010); print "\n"; # Perl double-precision floating point(32 bits) # Since it is represented by , all 32 bits are # Invert. print "4: Negation of binary 1100\n"; printf("%04b", ~ 0b1100); print "\n\n"; print "5: Use bitwise operations to indicate that all flags are on.\n"; sub FLG1 {1;} A subroutine that returns # 1. 1 is 1 in binary. A subroutine that returns sub FLG2 {2;} # 2. 2 is 10 in binary. A subroutine that returns sub FLG3 {4;} # 4. 4 is 100 in base. my $all_flg_on = FLG1 | FLG2 | FLG3; printf("%03b", $all_flg_on); print "\n\n"; print "6: Make sure FLG3 is on.\n"; if ($all_flg_on & FLG3) { print "FLG3 is turned on.\n"; }
printf("%04b", 0b1100 & 0b1010);
(1) Express numbers in binary
You can use the prefix 0b to represent numbers in binary.
(2) Output in binary notation
To output in binary, use printf function and specify%b in the format. 04 between%and b is an option that means to fill the missing part with 0 in 4-digit display.
(3) Expressing flags by bit operation
sub FLG1 {1;} A subroutine that returns # 1. 1 is 1 in binary. A subroutine that returns sub FLG2 {2;} # 2. 2 is 10 in binary. A subroutine that returns sub FLG3 {4;} # 4. 4 is 100 in base. my $all_flg_on = FLG1 | FLG2 | FLG3; printf("%03b", $all_flg_on); print "\n\n"; print "6: Make sure FLG3 is on.\n"; if ($all_flg_on & FLG3) { print "FLG3 is turned on.\n"; }
(3) - 1 Expressing a constant with a subroutine
sub FLG1 {1};
If you write this, 1 will be returned when subroutine is called, and you can treat it like a constant. When calling, you can call it with FGG1 without writing FLG1 ().
(3) - Set the flag value to a multiple of 2 so that the bits do not overlap
sub FLG1 {1;} A subroutine that returns # 1. 1 is 1 in binary. A subroutine that returns sub FLG2 {2;} # 2. 2 is 10 in binary. A subroutine that returns sub FLG3 {4;} # 4. 4 is 100 in base.
Use multiples of 2 to prevent the bits from overlapping. In order to use it as a flag, each bit must be able to be determined separately.
(3) - 3 Expression that all flags are turned on
my $all_flg_on = FLG1 | FLG2 | FLG3;
OR the bits you want to turn on.
(3) - 4 Make sure the flag is on
if ($all_flg_on & FLG3) { # ... }
You can check if the flag is on by taking the logical product with the flag that you want to check if it is on.
(3) - 5 Basically avoid bit operations
Basically, I don't write a program that uses bit operations. Bitwise operations are only needed when calling a Perl function that directly wraps a C library.
The significance of bit operations is to save memory and speed up processing. Since it can be judged with 1 bit, a lot of information can be expressed with a small number of bits. In addition, the processing speed is high because only one bit is judged for each condition.
The following is an example bit shift operation.
use strict; use warnings; my $num = 8; print "\$num = $num\n\n"; print "1: Left bit shift\n"; # Same as 2 times print "1 bit left shift:". $Num << 1 . "\n"; # Same as 2 squared print "2-bit left shift:". $Num << 2 . "\n"; # Same as 2 cubed print "3-bit left shift:". $Num << 3 . "\n"; print "\n"; print "2: Right bit shift\n"; # Same as the quotient divided by 2 print "1 bit right shift:". $Num >> 1 . "\n"; # Same as the quotient divided by 2 squared print "2-bit right shift:". $Num >> 2 . "\n"; # Same as the quotient of 2 divided by the cube print "3-bit right shift:". $Num >> 3 . "\n"; print "\n";
Binary to Decimal Conversion
00100100