ITP 165 Lecture 25: Bitwise Operators

36 views2 pages
ITP 165: Intro to C++ - Lecture 25: Bitwise Operators
Bitwise Operators
Bitwise operators operate on each bit of binary number(s) in a specific way
We will cover 4 different bitwise operators in C++:
~ bitwise NOT
& bitwise AND
| bitwise OR
^ bitwise XOR
Bitwise NOT operates on a single number, whereas AND, OR, XOR operate between
two numbers
Bitwise NOT
Bitwise NOT flips every bit in a single binary number
So something that was previously a 0 will instead be a 1, and viceversa
Bitwise AND
In a bitwise AND, you line up the binary digits of two numbers
Any places that are a 1 in both numbers are a 1 in the result
Bitwise OR
In a bitwise OR, you line up the binary digits of two numbers
Any places that are a 1 in either of the numbers are a 1 in the result
Bitwise XOR
In a bitwise XOR (exclusive or), you line up the binary digits of two numbers
Any places that are a 1 in one but only one of the numbers are a 1 in the result
Usages
● Bit-flips
Determining unknown bits
● Encryption/Decryption
● Graphics
Bit Shifts
Bit shift operation moves the sequence to the left or right
Many types of bit shifts
Focus on Arithmetic Shifts
Bit Shift Left
8 th bit (in the 7 spot) is shifted out, all bits shifted LEFT, 0 shifted in to make 8 bits (in
the 0 spot)
b = a << 1; (left shift)
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Itp 165: intro to c++ - lecture 25: bitwise operators. Bitwise operators operate on each bit of binary number(s) in a specific way. We will cover 4 different bitwise operators in c++: Bitwise not operates on a single number, whereas and, or, xor operate between two numbers. Bitwise not flips every bit in a single binary number. So something that was previously a 0 will instead be a 1, and viceversa. In a bitwise and, you line up the binary digits of two numbers. Any places that are a 1 in both numbers are a 1 in the result. In a bitwise or, you line up the binary digits of two numbers. Any places that are a 1 in either of the numbers are a 1 in the result. In a bitwise xor (exclusive or), you line up the binary digits of two numbers.

Get access

Grade+20% OFF
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers