Hướng dẫn dùng bitwise manipulation trong PHP
Hai phép toán cơ bản đối với hệ nhị phân là OR và AND. HOẶC có nghĩa là 'nếu A bật hoặc B bật'. Một ví dụ trong thế giới thực sẽ là hai thiết bị chuyển mạch song song. Nếu một trong hai cho phép dòng điện chạy qua, thì dòng điện đi qua. AND có nghĩa là 'nếu cả A và B đều bật'. Ví dụ trong thế giới thực là hai công tắc mắc nối tiếp. Dòng điện sẽ chỉ đi qua nếu cả hai đều cho phép dòng điện chạy qua. Trong máy tính, đây không phải là công tắc vật lý mà là chất bán dẫn và chức năng của chúng được gọi là cổng logic . Chúng làm những việc tương tự như công tắc - phản ứng với dòng điện hoặc không có dòng điện. Khi áp dụng cho số nguyên, mọi bit trong một số được kết hợp với mọi bit trong số kia. Vì vậy, để hiểu các toán tử theo chiều bit OR và AND, bạn cần chuyển các số thành nhị phân, sau đó thực hiện phép toán OR hoặc AND trên mọi cặp bit phù hợp. Đó là lý do tại sao:
Trong khi
Do đó, phép toán (& 1) so sánh bit ngoài cùng bên phải với 1 bằng cách sử dụng logic AND. Tất cả các bit khác được bỏ qua một cách hiệu quả bởi vì bất cứ điều gì VÀ không có gì là không có gì. Một số chẵn trong hệ nhị phân cũng là một số chẵn trong ký hiệu thập phân (10 là bội số của 2). Các hoạt động cơ bản khác đối với hệ thống nhị phân bao gồm NOT và XOR. NOT có nghĩa là 'nếu A tắt' và là dạng cổng logic duy nhất chỉ nhận một tín hiệu hoặc 'tham số' thay vì hai. XOR có nghĩa là 'nếu A hoặc B được bật, nhưng không phải cả hai'. Và sau đó là NAND, NOR và NXOR, về cơ bản KHÔNG được kết hợp với AND, OR và XOR, tức là NAND có nghĩa là 'nếu cả A và B đều không nằm trên'. Trong lập trình, toán tử
Những cái khác có thể được tạo thành bằng cách kết hợp chúng, ví dụ:
Ghi chú cụ thể về PHP Toán tử bitwise không hoạt động trên các giá trị dấu phẩy động
và trong PHP, các giá trị float sẽ được chuyển đổi ngầm thành số nguyên trước tiên. Các số bên ngoài phạm vi có thể được biểu thị dưới dạng số nguyên sẽ bị cắt ngắn thành 0 - nghĩa là tất cả các số trên PHP_INT_MAX sẽ trông "chẵn" trong biểu thức 27 hữu ích 1 bình luận chia sẻ Bitwise operators allow evaluation and manipulation of specific bits within an integer. Bitwise Operators
Bit shifting in PHP is arithmetic. Bits shifted off either end are discarded. Left shifts have zeros shifted in on the right while the sign bit is shifted out on the left, meaning the sign of an operand is not preserved. Right shifts have copies of the sign bit shifted in on the left, meaning the sign of an operand is preserved. Use parentheses to ensure the desired
precedence. For example, If both operands for the If the operand for the Both operands and the result for the PHP's error_reporting ini setting uses bitwise values,
providing a real-world demonstration of turning
bits off. To show all errors, except for notices,
the php.ini file instructions say to use:
This works by starting with E_ALL:
00000000000000000111011111111111
Then taking the value of E_NOTICE...
00000000000000000000000000001000
... and inverting it via Another way to accomplish that is using XOR ( error_reporting can also be used to demonstrate turning bits on.
The way to show just errors and recoverable errors is:
This process combines E_ERROR
00000000000000000000000000000001
and
00000000000000000001000000000000
using the OR ( Example #1 Bitwise AND, OR and XOR operations on integers
--------- --------- -- --------- The above example will output: --------- --------- -- --------- result value op test --------- --------- -- --------- Bitwise AND ( 0 = 0000) = ( 0 = 0000) & ( 5 = 0101) ( 1 = 0001) = ( 1 = 0001) & ( 5 = 0101) ( 0 = 0000) = ( 2 = 0010) & ( 5 = 0101) ( 4 = 0100) = ( 4 = 0100) & ( 5 = 0101) ( 0 = 0000) = ( 8 = 1000) & ( 5 = 0101) Bitwise Inclusive OR ( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101) ( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101) ( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101) ( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101) (13 = 1101) = ( 8 = 1000) | ( 5 = 0101) Bitwise Exclusive OR (XOR) ( 5 = 0101) = ( 0 = 0000) ^ ( 5 = 0101) ( 4 = 0100) = ( 1 = 0001) ^ ( 5 = 0101) ( 7 = 0111) = ( 2 = 0010) ^ ( 5 = 0101) ( 1 = 0001) = ( 4 = 0100) ^ ( 5 = 0101) (13 = 1101) = ( 8 = 1000) ^ ( 5 = 0101) Example #2 Bitwise XOR operations on strings
Example #3 Bit shifting on integers
"\n--- BIT SHIFT RIGHT ON NEGATIVE INTEGERS ---\n";$val = -4; Output of the above example on 32 bit machines: --- BIT SHIFT RIGHT ON POSITIVE INTEGERS --- Expression: 2 = 4 >> 1 Decimal: val=4 res=2 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000010 NOTE: copy of sign bit shifted into left side Expression: 1 = 4 >> 2 Decimal: val=4 res=1 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000001 Expression: 0 = 4 >> 3 Decimal: val=4 res=0 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000000 NOTE: bits shift out right side Expression: 0 = 4 >> 4 Decimal: val=4 res=0 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000000 NOTE: same result as above; can not shift beyond 0 --- BIT SHIFT RIGHT ON NEGATIVE INTEGERS --- Expression: -2 = -4 >> 1 Decimal: val=-4 res=-2 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111110 NOTE: copy of sign bit shifted into left side Expression: -1 = -4 >> 2 Decimal: val=-4 res=-1 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111111 NOTE: bits shift out right side Expression: -1 = -4 >> 3 Decimal: val=-4 res=-1 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111111 NOTE: same result as above; can not shift beyond -1 --- BIT SHIFT LEFT ON POSITIVE INTEGERS --- Expression: 8 = 4 << 1 Decimal: val=4 res=8 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000001000 NOTE: zeros fill in right side Expression: 1073741824 = 4 << 28 Decimal: val=4 res=1073741824 Binary: val=00000000000000000000000000000100 res=01000000000000000000000000000000 Expression: -2147483648 = 4 << 29 Decimal: val=4 res=-2147483648 Binary: val=00000000000000000000000000000100 res=10000000000000000000000000000000 NOTE: sign bits get shifted out Expression: 0 = 4 << 30 Decimal: val=4 res=0 Binary: val=00000000000000000000000000000100 res=00000000000000000000000000000000 NOTE: bits shift out left side --- BIT SHIFT LEFT ON NEGATIVE INTEGERS --- Expression: -8 = -4 << 1 Decimal: val=-4 res=-8 Binary: val=11111111111111111111111111111100 res=11111111111111111111111111111000 NOTE: zeros fill in right side Expression: -2147483648 = -4 << 29 Decimal: val=-4 res=-2147483648 Binary: val=11111111111111111111111111111100 res=10000000000000000000000000000000 Expression: 0 = -4 << 30 Decimal: val=-4 res=0 Binary: val=11111111111111111111111111111100 res=00000000000000000000000000000000 NOTE: bits shift out left side, including sign bit Output of the above example on 64 bit machines: --- BIT SHIFT RIGHT ON POSITIVE INTEGERS --- Expression: 2 = 4 >> 1 Decimal: val=4 res=2 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000010 NOTE: copy of sign bit shifted into left side Expression: 1 = 4 >> 2 Decimal: val=4 res=1 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000001 Expression: 0 = 4 >> 3 Decimal: val=4 res=0 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: bits shift out right side Expression: 0 = 4 >> 4 Decimal: val=4 res=0 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: same result as above; can not shift beyond 0 --- BIT SHIFT RIGHT ON NEGATIVE INTEGERS --- Expression: -2 = -4 >> 1 Decimal: val=-4 res=-2 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111110 NOTE: copy of sign bit shifted into left side Expression: -1 = -4 >> 2 Decimal: val=-4 res=-1 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111111 NOTE: bits shift out right side Expression: -1 = -4 >> 3 Decimal: val=-4 res=-1 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111111 NOTE: same result as above; can not shift beyond -1 --- BIT SHIFT LEFT ON POSITIVE INTEGERS --- Expression: 8 = 4 << 1 Decimal: val=4 res=8 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000001000 NOTE: zeros fill in right side Expression: 4611686018427387904 = 4 << 60 Decimal: val=4 res=4611686018427387904 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0100000000000000000000000000000000000000000000000000000000000000 Expression: -9223372036854775808 = 4 << 61 Decimal: val=4 res=-9223372036854775808 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=1000000000000000000000000000000000000000000000000000000000000000 NOTE: sign bits get shifted out Expression: 0 = 4 << 62 Decimal: val=4 res=0 Binary: val=0000000000000000000000000000000000000000000000000000000000000100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: bits shift out left side --- BIT SHIFT LEFT ON NEGATIVE INTEGERS --- Expression: -8 = -4 << 1 Decimal: val=-4 res=-8 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1111111111111111111111111111111111111111111111111111111111111000 NOTE: zeros fill in right side Expression: -9223372036854775808 = -4 << 61 Decimal: val=-4 res=-9223372036854775808 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=1000000000000000000000000000000000000000000000000000000000000000 Expression: 0 = -4 << 62 Decimal: val=-4 res=0 Binary: val=1111111111111111111111111111111111111111111111111111111111111100 res=0000000000000000000000000000000000000000000000000000000000000000 NOTE: bits shift out left side, including sign bit Warning Use functions from the gmp extension for bitwise manipulation on numbers beyond wbcarts at juno dot com ¶ 10 years ago
setFlag($flag, $value) grayda dot NOSPAM at DONTSPAM dot solidinc dot org ¶ 13 years ago
m0sh at hotmail dot com ¶ 14 years ago
frankemeks77 at yahoo dot com ¶ 10 years ago
ASchmidt at Anamera dot net ¶ 3 years ago
COMBO_BC = FLAG_B | FLAG_C;$bitmask = 0b000;// Setting individual flags. zlel grxnslxves13 at hotmail dot com~=s/x/ee/g ¶ 16 years ago
$a1 ^ $b1) |($a2 ^ $b2)) + $c; S?b. ¶ 17 years ago
zooly at globmi dot com ¶ 12 years ago
bindec(substr($binary, $bits).substr($binary, 0, $bits)) zewt at hotmail dot com ¶ 15 years ago
Silver ¶ 13 years ago
amckenzie4 at gmail dot com ¶ 12 years ago
vivekanand dot pathak25 at gmail dot com ¶ 9 years ago
icy at digitalitcc dot com ¶ 17 years ago
DEFINE('INTEGER_LENGTH',31); // Stupid signed bit.class bitmask forlamp at msn dot com ¶ 14 years ago
ivoras at gmail dot com ¶ 11 years ago
spencer-p-moy at example dot com ¶ 10 years ago
josh at joshstrike dot com ¶ 11 years ago
masksOf($n,$bits) {
cw3theophilus at gmail dot com ¶ 13 years ago
aba at example dot com ¶ 11 years ago
erich at seachawaii dot com ¶ 9 years ago
Adam ¶ 12 years ago
sag at ich dot net ¶ 9 years ago
Anonymous ¶ 12 years ago
Bob ¶ 13 years ago
Tbrendstrup ¶ 16 years ago
Eric Swanson ¶ 17 years ago
Core Xii ¶ 12 years ago
Anonymous ¶ 10 years ago
biziclop at vipmail dot hu ¶ 2 years ago
'This PHP uses ' . count_set_bits(-1) .'-bit integers.'; joey ¶ 6 years ago
bartbons at debster.nl ¶ 14 years ago
Nina Cording ¶ 16 years ago
bitRotate($value,$amount,$bits) { luis at rosety dot com ¶ 6 years ago
J. Ketting ¶ 8 years ago
?> |