Operadores PHP bitwise

¿Qué son operadores Bitwise

Explanation

Los operadores Bitwise son usados para activar o desactivar bits dentro de un entero.

Ejemplo




<?php
echo 12
9 ; // Outputs '5'
echo 2
"3"; // Outputs 1
echo 2
"3"; // Outputs 1
?>


En el ejemplo anterior el binario de 12 es 1100, para 9 es 1001, cuando el operador XOR se utiliza dará un valor de 101 que es 5 en números decimales.

Si los parámetros de la izquierda y la derecha son cadenas, mostrará los caracteres ASCII

Los siguientes son los operadores bitwise disponibles
Ejemplo Nombre Resultado
$a & $b And Bits that are set in both $a and $b are set.
$a | $b Or Bits that are set in either $a or $b are set.
$a ^ $b Xor Bits that are set in $a or $b but not both are set.
~ $a Not Bits that are set in $a are not set, and vice versa.
$a<<$b Shift left Shift the bits of $a $b steps to the left (each step means "multiply by two")
$a>>$b Shift right Shift the bits of $a $b steps to the right (each step means "divide by two")

Php Tutorial