Bitwise Operators in Programming
In programming, understanding bitwise logical and shift operators is essential for manipulating individual bits efficiently. This involves creating and using masks to operate on data at the bit level, enabling tasks such as setting, clearing, or toggling specific bits. Explore how these operators work and how to implement them effectively in your code.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Bitwise Operators Objectives To be able to use the bitwise logical operators in programs To be able to use the bitwise shift operators in programs To understand how to create and use masks to manipulate bits Computer Science: A Structured Programming Approach Using C 1
Exact Size Integer Types The integer types, such as int and long, are machine dependent. In one computer, the size of int may be four bytes; in another computer it may be two bytes. While many bitwise applications work well on machine-dependent integer types, other applications need to assure that the size is fixed. C allows us to define integer types of sizes 8, 16, 32, and 64 bits. Computer Science: A Structured Programming Approach Using C 2
Table 1 Fixed-size Integer Types Computer Science: A Structured Programming Approach Using C 3
Logical Bitwise Operators The logical operators look at data as individual bits to be manipulated. Four operators are provided to manipulate bits: bitwise and(&), bitwise inclusive or(|), bitwise exclusive or(^), and one s complement (~). The first three are binary operators; the one s complement is a unary operator. Topics discussed in this section: Bitwise and Operator Bitwise Inclusive or Operator Bitwise Exclusive or Operator One s Complement Operator Computer Science: A Structured Programming Approach Using C 4
And Truth Table Table 2 Computer Science: A Structured Programming Approach Using C 5
PROGRAM 1 Simple Bitwise And Demonstration Computer Science: A Structured Programming Approach Using C 6
PROGRAM 1 Simple Bitwise And Demonstration Computer Science: A Structured Programming Approach Using C 7
Inclusive Or Truth Table Table 3 Computer Science: A Structured Programming Approach Using C 8
Simple Inclusive or Demonstration PROGRAM 2 Computer Science: A Structured Programming Approach Using C 9
Simple Inclusive or Demonstration PROGRAM 2 Computer Science: A Structured Programming Approach Using C 10
Exclusive Or Truth Table Table 4 Computer Science: A Structured Programming Approach Using C 11
Simple Exclusive or Demonstration PROGRAM 3 Computer Science: A Structured Programming Approach Using C 12
Simple Exclusive or Demonstration PROGRAM 3 Computer Science: A Structured Programming Approach Using C 13
Table 5 One s Complement Truth Table Computer Science: A Structured Programming Approach Using C 14
PROGRAM 4 One s Complement Computer Science: A Structured Programming Approach Using C 15
PROGRAM 4 One s Complement Computer Science: A Structured Programming Approach Using C 16
FIGURE 1 Checksum Calculation Computer Science: A Structured Programming Approach Using C 17
PROGRAM 5 Demonstrate Checksum Computer Science: A Structured Programming Approach Using C 18
PROGRAM 5 Demonstrate Checksum Computer Science: A Structured Programming Approach Using C 19
Shift Operators The shift operators move bits to the right or the left. When applied to unsigned numbers, these operators are implementation independent. When used with signed numbers, however, the implementation is left to the discretion of the software engineer who designs the compiler. Topics discussed in this section: Shift Rotation Computer Science: A Structured Programming Approach Using C 20
FIGURE 2 Shift-right Operation Computer Science: A Structured Programming Approach Using C 21
PROGRAM 6 Simple Shift-right Demonstration Computer Science: A Structured Programming Approach Using C 22
PROGRAM 6 Simple Shift-right Demonstration Computer Science: A Structured Programming Approach Using C 23
PROGRAM 6 Simple Shift-right Demonstration Computer Science: A Structured Programming Approach Using C 24
Table 6 Divide by Shift Computer Science: A Structured Programming Approach Using C 25
FIGURE 3 Shift-left Operation Computer Science: A Structured Programming Approach Using C 26
PROGRAM 7 Simple Shift-left Demonstration Computer Science: A Structured Programming Approach Using C 27
PROGRAM 7 Simple Shift-left Demonstration Computer Science: A Structured Programming Approach Using C 28
Table 7 Multiply by Shift Computer Science: A Structured Programming Approach Using C 29
FIGURE 4 Right and Left Rotation Computer Science: A Structured Programming Approach Using C 30
PROGRAM 8 Rotate Left and Right Test Driver Computer Science: A Structured Programming Approach Using C 31
PROGRAM 8 Rotate Left and Right Test Driver Computer Science: A Structured Programming Approach Using C 32
PROGRAM 8 Rotate Left and Right Test Driver Computer Science: A Structured Programming Approach Using C 33