← Back to PHP Course | Chapter 3: Operators | Lesson 4 of 8

PHP Bitwise Operators

Bitwise operators किसी number की total value के बजाय उसके अंदर छोटे-छोटे on-and-off switches को देखते हैं। यह पूरा number पढ़ने के बजाय एक row में individual light switches flip करने जैसा है।
Syntax
php
$result = $a & $b;   // AND
$result = $a | $b;   // OR
$result = $a ^ $b;   // XOR
$result = ~$a;       // NOT (flip every bit)

Bitwise AND और OR

Bitwise operators किसी number की overall value के बजाय उसके individual binary digits पर काम करते हैं।

& हर output bit को सिर्फ वहाँ 1 set करता है जहाँ *दोनों* input bits 1 थीं; | हर output bit को वहाँ 1 set करता है जहाँ *कोई एक* input bit 1 थी — वही logic जो &&/|| का है, बस bit by bit apply किया गया, पूरे booleans के बजाय।

उदाहरण: Bitwise AND and OR

php
<?php
echo 6 & 3, "\n"; // AND
echo 6 | 3;       // OR
?>

Bitwise XOR (^)

^ (XOR) हर bit को exactly वहाँ 1 set करता है जहाँ दोनों inputs disagree करते हैं — एक 1 है और दूसरा 0 है। जहाँ bits match करती हैं, चाहे दोनों 0 हों या दोनों 1, result bit 0 होता है।

यह bit-by-bit 'exactly one' behavior ही XOR को flags on और off toggle करने के लिए उपयोगी बनाता है।

उदाहरण: Bitwise XOR (^)

php
<?php
// Print `6 ^ 3` to the output
echo 6 ^ 3;
?>

Bitwise NOT (~)

~ एक operation में किसी number की हर bit flip कर देता है, हर 1 को 0 में और हर 0 को 1 में बदलते हुए।

Binary में negative numbers represent होने के तरीके (two's complement) की वजह से, यह mathematically -(x + 1) के बराबर हो जाता है — एक detail जो लोगों को तब तक confuse करती है जब तक वे किसी example को trace नहीं कर लेते।

उदाहरण: Bitwise NOT (~)

php
<?php
// Print `~5` to the output
echo ~5;
?>

Shift Operators (<<, >>)

<< किसी number की bits को left shift करता है, दाईं तरफ खाली हुई positions को zeros से भरते हुए — किसी power of two से multiply करने जैसा ही effect।

>> bits को right shift करता है, जिसका effect किसी power of two से divide करके किसी भी remainder को discard करने जैसा है, दोनों को 2 से multiplication या division के बहुत तेज़ alternatives बनाते हुए।

उदाहरण: Shift Operators (<<, >>)

php
<?php
echo 4 << 1, "\n"; // multiply by 2
echo 4 >> 1;       // divide by 2
?>

Bitwise Flags का इस्तेमाल

एक common real इस्तेमाल है कई independent yes/no flags को एक integer में pack करना, हर एक को एक single bit से represent करते हुए — $permissions & READ_FLAG check करना आपको बताता है कि वह एक flag set है या नहीं, हर flag के लिए एक अलग variable की ज़रूरत के बिना।

उदाहरण: Bitwise Flags Usage

php
<?php
// Call `define('READ_FLAG', 1)`
define('READ_FLAG', 1);
// Call `define('WRITE_FLAG', 2)`
define('WRITE_FLAG', 2);
// Declare `$permissions`, set to `READ_FLAG | WRITE_FLAG`
$permissions = READ_FLAG | WRITE_FLAG;
// Print a detailed dump (with types) of `(bool)($permissions & READ_FLAG)`
var_dump((bool)($permissions & READ_FLAG));
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. & (bitwise AND) को && (logical AND) से confuse करना, ताकि 5 & 2 एक boolean के बजाय 0 दे।
  2. यह surprise होना कि ~5 -6 है, क्योंकि PHP integers signed हैं और ~ sign bit सहित हर bit flip करता है।
  3. एक negative amount से shift करना, जो PHP 7 और बाद में एक ArithmeticError throw करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.