
Understanding Bitwise Operators in Programming
Learn about bitwise operators that work on individual bit values to represent data efficiently in programming. Explore how AND, OR, and XOR operations manipulate binary numbers, along with left shift operations in languages like C/C++.
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 Like logical operators Work on the individual bit values that represent some data Useful in programs to represent multiple Boolean values (or flags ) in a single numeric datatype More on that later
AND Usually represented with & Notice the difference from && which is the logical AND The & operation checks the bit positions of two binary numbers If both values are a 1, the result of the & is also a 1 For all other values, the result is 0
AND Usually represented with & Notice the difference from && which is the logical AND Example: 0001 1110 & 1101 0110 ------------------ 0001 0110 The & operation checks the bit positions of two binary numbers If both values are a 1, the result of the & is also a 1 For all other values, the result is 0
OR Usually represented with | Notice the difference from || which is the logical OR The | operation checks the bit positions of two binary numbers If either values are a 1, the result of the | is also a 1 If both values are 0, the result is 0
OR Usually represented with | Notice the difference from || which is the logical OR Example: 0001 1110 | 1101 0110 ------------------ 1101 1110 The | operation checks the bit positions of two binary numbers If either values are a 1, the result of the | is also a 1 If both values are 0, the result is 0
Exclusive OR Written as XOR or as ^ Example: The ^ operation checks the bit positions of two binary numbers 0001 1110 ^ 1101 0110 ------------------ 1100 1000 If neither value is the same, the result of the ^ is a 1 If both values are the same, the result is 0
Left Shift Written as << Takes a binary number and moves all binary values to the left by one In languages like C/C++ we can provide a value of how many bit positions we want to shift << 2
Left Shift Written as << Example: Takes a binary number and moves all binary values to the left by one 0001 1110 << 2 ------------------ 0111 1000 In languages like C/C++ we can provide a value of how many bit positions we want to shift << 2
Left Shift Written as << Example: Takes a binary number and moves all binary values to the left by one 0001 1110 << 2 ------------------ 0111 1000 In languages like C/C++ we can provide a value of how many bit positions we want to shift << 2 NOTE: Left shift is that same as multiplying by 2 why?
Right Shift Written as >> Example: Takes a binary number and moves all binary values to the right by one 0001 1110 >> 2 ------------------ 0000 0111 In languages like C/C++ we can provide a value of how many bit positions we want to shift >> 2 NOTE: Left shift is that same as dividing by 2 why?
Bit Masking Let s return to the idea of using bits as flags Suppose we have a program with 8 options represented by an integer flags We can use shift to create bit values for the options Use OR (|) to set the options on flags Use XOR (^) to unset options Use AND (&) to check what options ARE set