Skip to content

Mayankyadav-018/Bitwise-Operators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

EXPERIMENT NO. 4

  • Aim: To study and implement bitwise operators in C++ and understand how binary-level operations are performed.

  • Tools used : IDE, Bitwise operators, arithmetic operators.

  • Theory: Bitwise operators in C++ are used to perform operations on individual bits of integer data. These operators are particularly useful in low-level programming, optimization tasks, cryptography, hardware interfacing, and situations requiring direct binary manipulation. Bitwise operations are faster and more efficient compared to arithmetic operations, especially in system-level programming.

๐Ÿ”น Bitwise Operators in C++ โ€“ Theory (Definition Style):

  1. & (Bitwise AND): Performs a logical AND operation on each bit of the two operands. The result bit is 1 only if both corresponding bits are 1. ๐Ÿ”ธ Example: c = a & b;

  2. | (Bitwise OR): Performs a logical OR operation on each bit of the operands. The result bit is 1 if at least one of the corresponding bits is 1. ๐Ÿ”ธ Example: c = a | b;

  3. ^ (Bitwise XOR): Performs a logical XOR (exclusive OR). The result bit is 1 only if the corresponding bits of the operands are different. ๐Ÿ”ธ Example: c = a ^ b;

  4. *~ (Bitwise NOT)*: Performs a bitwise complement (1's complement), flipping each bit of the operand (i.e., 0 becomes 1, and 1 becomes 0). ๐Ÿ”ธ Example: c = ~a;

  5. << (Left Shift): Shifts the bits of the operand to the left by the specified number of positions. Each left shift multiplies the number by 2. ๐Ÿ”ธ Example: c = a << 1;

  6. >> (Right Shift): Shifts the bits of the operand to the right by the specified number of positions. Each right shift divides the number by 2. ๐Ÿ”ธ Example: c = a >> 2;

Use of bitwise operators:

This C++ program demonstrates the use of bitwise operators such as AND, OR, NOT, XOR, left shift, and right shift using two predefined integers. It calculates and displays the result of each operation, helping students understand how data is manipulated at the binary level.

1> Start

2> Declare and initialize two integer variables: a, b.

3> Perform bitwise operations:

  • bitwise_and = a & b

  • bitwise_or = a | b

  • bitwise_not = ~a

  • bitwise_xor = a ^ b

  • left_shift = a << 2

  • right_shift = a >> 1

4> Display the results of each operation using cout

5> End

Set and Reset Specific Bit position in an integer:

This C++ program demonstrates how to set and reset (clear) specific bits of an integer using bitwise shift and bitwise OR/AND/NOT operations. It takes bit positions as input and modifies the original number accordingly.

1> Start

2> Declare and initialize an integer variable a = 24

3> Take input for the bit position to be set โ†’ store in bit_to_be_set

4> Perform setting using: set = a | (1 << bit_to_be_set)

5> Take input for the bit position to be reset โ†’ store in bit_to_be_reset

6> Perform resetting using: reset = a & ~(1 << bit_to_be_reset)

7> Display the results using cout

8> End

  • Conclusion: In this experiment, we studied various bitwise operators in C++ such as AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). We learned how these operators work at the binary level and how they manipulate individual bits of integers. By writing and executing simple programs, we understood their practical usage and importance in low-level programming and optimization. This experiment helped us strengthen our understanding of binary operations in C++.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages