Operatori pe biți
Operatorii pe biți compară numere în forma lor binară:
Tabelul operatorilor pe biți
main.py
# Operator | Name | Description | Example
# & | AND | Sets each bit to 1 if both bits are 1 | x & y
# | | OR | Sets each bit to 1 if one of two bits is 1 | x | y
# ^ | XOR | Sets each bit to 1 if only one of two bits is 1 | x ^ y
# ~ | NOT | Inverts all the bits | ~x
# << | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off | x << 2
# >> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | x >> 2
Exemple
Exemplu
& analizează fiecare pereche de biți. Bitul rezultat este 1 când ambii sunt 1 și 0 în orice alt caz:
Prompt
Scrie un program Python care folosește operatorul pe biți AND (&) asupra numerelor 6 și 3 și afișează rezultatul.
Ieșire: program Python complet
main.py
print(6 & 3)
Exemplu
| analizează fiecare pereche de biți. Bitul rezultat este 1 când cel puțin unul dintre ei este 1 și 0 în caz contrar:
Prompt
Scrie un program Python care folosește operatorul pe biți OR (|) asupra numerelor 6 și 3 și afișează rezultatul.
Ieșire: program Python complet
main.py
print(6 | 3)
Exemplu
^ analizează fiecare pereche de biți. Bitul rezultat este 1 când exact unul dintre ei este 1. Dacă sunt amândoi 1 sau amândoi 0, este 0:
Prompt
Scrie un program Python care folosește operatorul pe biți XOR (^) asupra numerelor 6 și 3 și afișează rezultatul.
Ieșire: program Python complet
main.py
print(6 ^ 3)