How does the bitwise operator work in PHP?

The bitwise operator in PHP allows for operations on individual bits within integers. It is commonly used for tasks such as setting or clearing specific bits, checking if a bit is set, or shifting bits left or right. To use bitwise operators in PHP, you need to understand how they manipulate binary representations of numbers.

// Example of using bitwise operators in PHP
$number1 = 5; // 00000101 in binary
$number2 = 3; // 00000011 in binary

// Bitwise AND
$result = $number1 & $number2; // Result: 00000001 (1 in decimal)

// Bitwise OR
$result = $number1 | $number2; // Result: 00000111 (7 in decimal)

// Bitwise XOR
$result = $number1 ^ $number2; // Result: 00000110 (6 in decimal)

// Bitwise NOT
$result = ~$number1; // Result: 11111010 (-6 in decimal)