0% found this document useful (0 votes)
4 views

Untitled Document (49)

Uploaded by

farooqurwa33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Untitled Document (49)

Uploaded by

farooqurwa33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

BITWISE OPERATORS

Introduction to Bitwise Operators

● Definition: Bitwise operators perform operations on individual bits of integers.


● Purpose: Used for low-level programming, performance optimization, and bit
manipulation.

VALUES OF 4 BIT

Decimal Binary

0 0000

1 0001

2 0010

3 0011

4 0100

5 0101

6 0110

7 0111

8 1000

9 1001

10 1010

11 1011

12 1100

13 1101
14 1110

15 1111

Types of Bitwise Operators

● Bitwise AND (&)


● Bitwise OR (|)
● Bitwise XOR (^)
● Bitwise NOT (~)
● Left Shift (<<)
● Right Shift (>>)

Bitwise AND (&)

● Description: Compares each bit and returns 1 if both bits are 1.

int a = 5; // Binary: 0101


int b = 3; // Binary: 0011
int result = a & b;

// Binary: 0001 (Decimal: 1)

cout << "a & b: " << result << endl;

=================================

Bitwise OR (|)
● Description: Compares each bit and returns 1 if at least one bit is 1.

int a = 5; // Binary: 0101


int b = 3; // Binary: 0011
int result = a | b; // Binary: 0111 (Decimal: 7)
cout << "a | b: " << result << endl;

=======================

Bitwise XOR (^)

● Description: Compares each bit and returns 1 if one of the bits is 1 (but not
both).

int a = 5; // Binary: 0101


int b = 3; // Binary: 0011
int result = a ^ b; // Binary: 0110 (Decimal: 6)
cout << "a ^ b: " << result << endl;

================

Bitwise NOT (~)

● Description: Flips all the bits (1s to 0s and vice versa).

int a = 5; // Binary: 0101


int result = ~a; // Binary: 1010 (Decimal: -6 in 2's
complement)
cout << "~a: " << result << endl;
====================

Here's a table comparing the left shift and right shift operations, illustrating their effects on
binary values:

Operation Example Binary Binary After Decimal Decimal


Before Shift Before After
Shift

Left Shift 5 << 2 0000 0101 0001 0100 5 20


(<<)

Right Shift 20 >> 2 0001 0100 0000 0101 20 5


(>>)

Key Points:

● Left Shift (<<):


○ Action: Moves bits to the left.
○ Effect: Multiplies the number by powers of 2 (e.g., shifting left by 1 multiplies by
2).
○ Example: 5 << 2 gives 20.
● Right Shift (>>):
○ Action: Moves bits to the right.
○ Effect: Divides the number by powers of 2 (e.g., shifting right by 1 divides by 2).
○ Example: 20 >> 2 gives 5.
================================
LEFT SHIFT
#include <iostream>

using namespace std;

int main() {

int number = 5; // Binary: 0000 0101

int shiftBy = 2; // Number of positions to shift

// Display original number

cout << "Original number: " << number << endl;

cout << "Binary representation: " << bitset<8>(number) << endl; //


Display binary

// Left shift operation

int result = number << shiftBy; // Shifting left by 2 positions

// Display the result

cout << "After left shifting by " << shiftBy << " positions:" << endl;

cout << "Resulting number: " << result << endl; // Decimal result

cout << "Binary representation: " << bitset<8>(result) << endl; // Display
binary
return 0;

=======================
RIGHT SHIFT

#include <iostream>
#include <bitset>
using namespace std;

int main() {
int number = 20; // Binary: 0001 0100
int shiftBy = 2; // Number of positions to shift

// Display original number


cout << "Original number: " << number << endl;
cout << "Binary representation: " << bitset<8>(number) << endl; // Display binary

// Right shift operation


int result = number >> shiftBy; // Shifting right by 2 positions

// Display the result


cout << "After right shifting by " << shiftBy << " positions:" << endl;
cout << "Resulting number: " << result << endl; // Decimal result
cout << "Binary representation: " << bitset<8>(result) << endl; // Display binary

return 0;
}

======================================

Practical Applications of Bitwise Operators


● Masking: Extracting or modifying specific bits.
● Flags: Efficiently managing multiple boolean flags.
● Performance: Faster than arithmetic operations.
● Cryptography: Used in various algorithms.
● Graphics Programming: Manipulating pixel data.

Complete Example Program


#include <iostream>
using namespace std;

int main() {
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011

cout << "a & b: " << (a & b) << endl; // Bitwise AND
cout << "a | b: " << (a | b) << endl; // Bitwise OR
cout << "a ^ b: " << (a ^ b) << endl; // Bitwise XOR
cout << "~a: " << (~a) << endl; // Bitwise NOT
cout << "a << 1: " << (a << 1) << endl; // Left shift
cout << "a >> 1: " << (a >> 1) << endl; // Right shift

return 0;
}

a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2
=================================================

You might also like