Home

Public Domain Photos

Free for commercial redistribution and alteration (All photo from http://publicphoto.org/ is 100% free for use)

PHP Bitwise Operators

OperatorDescriptionResult
$a & $bAndBits that are set in both $a
and $b are set.
$a | $bOr (inclusive or)Bits that are set in either $a
or $b are set.
$a ^ $bXor (exclusive or)Bits that are set in $a
or $b but not both are set.
$a << $bShift leftShift the bits of $a
$b steps to the left (each step means
“multiply by two”)
$a >> $bShift rightShift the bits of $a
$b steps to the right (each step means
“divide by two”)
~ $aNotBits that are set in $a
are not set, and vice versa.

php logoBitwise operators allow evaluation and manipulation of specific bits within an integer. If both the left-hand and right-hand parameters are strings, the bitwise operator will operate on the characters’ ASCII values.

Warning: Don’t right shift for more than 32 bits on 32 bits systems. Don’t left shift in case it results to number longer than 32 bit. Use functions from the gmp extension ( gmp_and(), gmp_or(), gmp_xor(), gmp_testbit(), gmp_clrbit() ) for bitwise manipulation on numbers beyond PHP_INT_MAX.

[ Short URL ... ]

2 Responses to “PHP Bitwise Operators”

  1. [...] Testing odd or even numbers with modulo or php bitwise operators: [...]

Leave a Reply

 

Powered by WordPress ::