Skip to content

Latest commit

 

History

History
34 lines (23 loc) · 934 Bytes

File metadata and controls

34 lines (23 loc) · 934 Bytes

Bitwise Operations in Python

Introduction

Bitwise operators in Python are used to perform operations on individual bits of binary numbers. These operators include bitwise AND, OR, XOR, and more.

List of Bitwise Operators

  1. Bitwise AND (&): Performs a bitwise AND operation on the binary representations of the operands.
  2. Bitwise OR (|): Performs a bitwise OR operation.
  3. Bitwise XOR (^): Performs a bitwise XOR operation.
  4. Bitwise NOT (~): Flips the bits of the operand, changing 0 to 1 and 1 to 0.
  5. Left Shift (<<): Shifts the bits to the left by a specified number of positions.
  6. Right Shift (>>): Shifts the bits to the right.

Examples

Bitwise AND

a = 5  # Binary: 0101
b = 3  # Binary: 0011
result = a & b  # Result: 0001 (Decimal: 1)

Bitwise OR

x = 10  # Binary: 1010
y = 7   # Binary: 0111
result = x | y  # Result: 1111 (Decimal: 15)