Bitwise AND (&), OR (|), XOR (^) and NOT (~) in C++

preview_player
Показать описание


This video is sponsored by Skillshare.
Рекомендации по теме
Комментарии
Автор

I did it. I finally watched every single video in the C++ playlist. I'm gonna go write the universe now.

anixias
Автор

Btw XOR is commonly used in encryption/decryption since it is reversable. plain text XOR key -> cipher text. cipher text XOR key -> plain text. So through that you can encrypt and decrypt with the same key

Netrole
Автор

Kind of fitting that the bitwise not (~) gets used to identify the destructor in C++

Like it then reads not(Constructor)
Which is kinda neat in my opinion.

ididauni
Автор

The Yan Dynasty!!! We love your videos Cherno, Please never stop. You absolutely have no idea how much help this series has been to me.

parikshitdubey
Автор

I am so glad you are doing more C++ videos. Exactly what I was looking for!

paintfortauva
Автор

Proudly speaking, I HAVE FINISHED ALL COURSES IN THIS WATCH LIST! I don't know what to say, I started Monday of this week and finished 8 hours before the new year eve. This is a great course, and there is a great amount of knowledge I have obtained before the end of 2021! I hope I will do great in 2022, with all this knowledge and a great pile of notes/codes that I wrote while taking this course! I thank you so much! this course is among the BEST of the BEST! I didn't just learnt those skills and tricks, but I was also able to understand the structure and fundamental of this language: knowing that nothing is permitted and everything can be true in C++. It is the best new year gift I have earned towards all this week's hard work! I thank you so much! And I hope I will do GREAT in 2022!

jeremygong
Автор

Example:

Images are arrays of pixels which can be represented by the 4 channels Red, Green, Blue, Alpha (transparency) or RGBA. We can use a uint32_t for this, so that each channel has 8 bits and has 2^8=256 values at its disposal. Examples are:

- name (RGBA notation) (bit notation with spaces for readability) (decimal notation)
- Fully opaque red: (255, 0, 0, 255) (4278190335)
- Fully opaque white: (255, 255, 255, 255) (4294967295)
- Fully opaque black: (0, 0, 0, 255) (255)
- Transparent yellow: (0, 255, 255, 128) (4278190335)
- Invisible blue: (0, 255, 255, 0) (4278190335)

Let's get the green value of a random pixel: 10011100 01001110 00111001 01010010 (2622372178)
- We use a mask which will extract only the green bits by shifting 255 to the green channel 16 bits to the left: (16711680)
- We use '&', which will result in only the green channel remaining: 01001110 (5111808)
- We shift with '>>' the green values to the beginning: 01001110 (78)
The result is 78

Let's implement it.

#include <iostream>

// Let's avoid magic numbers.
#define RED_OFFSET 32;
#define RED_MASK (255 << RED_OFFSET) // Which is 4278190080.

#define GREEN_OFFSET 16
#define GREEN_MASK (255 << GREEN_OFFSET) // Which is 16711680 as we determined earlier.

#define BLUE_OFFSET 8
#define BLUE_MASK (255 << BLUE_OFFSET) // Which is 65280.

#define ALPHA_OFFSET 0
#define ALPHA_MASK 255 // Is already at the beginning so no shifting

uint32_t get_green(uint32_t color)
{
return (color & GREEN_MASK) >> GREEN_OFFSET;
}

// BONUS. Can you figure out what happens?
uint32_t set_green(uint32_t color, uint8_t green) // NOTE: We pass a 8-bit value for green to limit the input to [0-255].
{
return (~GREEN_MASK & color) | green << GREEN_OFFSET;
}

int main()
{
std::cout << get_green(2622372178) << std::endl; This should print 78, like described above.
std::cout << get_green(set_green(4278190335, 99)) << std::endl; // This should print 99.

return 0;
}

JMRC
Автор

ha ha "don't go ahead and actually do this, please don't ", "maybe it will be fast" lol.
That remind me Deadpool movie....

kavindaravishan
Автор

You can also use XOR to flip a certain part of a variable.

For example flipping only 4 bits of int8_t. 1100 1010 ^ 0000 1111 = 1100 0101

Akniy
Автор

I really like this a lot! Super informative! For bitwise XOR I have used it for spatial hashing algorithm, super useful too! And masking with bitwise AND is the perfect way to describe it!!!

voxelltech
Автор

Very well presented, understood all of it without questions. Thank you!

XeZrunner
Автор

Incredibly helpful! Thank you lots for providing detailed examples of using the operators. I much better understand why we use `n = n & (n-1)` to unset the rightmost set bit after watching this video, as its really just a bit mask to change set bits to 0.

gnawlix
Автор

useful XOR example:
if you have 2 arrays that are exactly the same except that one has an extra element, finding this extra element can be done by XORing all the numbers from the arrays starting at 0. This solution may be the only one that has a constant space complexity.

int extraElem(const std::vector<int>& a, const std::vector<int>& b) {
int elem = 0;
for (const int& num : a) {
elem ^= num;
}
for (const int& num : b) {
elem ^= num;
}
return elem;
}

alexandrelipp
Автор

I really appreciate the content you put out, and really surprised you don't have a ton more likes and followers

pastasawce
Автор

Finally! Waiting for this for a long time

SankoshSaha_
Автор

Thank you, Cherno. Your's video are awesome!

justcode
Автор

Hey Cherno, can you do a mini series on C++ design patterns apart from the singleton one? Would be really helpful.

TapanKumar-vole
Автор

This timing is amazing! Just the other day I was looking at some C++ source code that used the OR ("|") operator and I had never seen it before so I had no idea what I was looking at. Thanks!

Schytheron
Автор

4:32 The logical AND (&&) allows you to compare integers in C++, and every number other than 0 represents true. The bitwise AND (&) will compare the bits of the numbers though. Because of that, comparing the same numbers with logical AND and bitwise AND can return different results.
For example the output of the following code will be 1 and 0 even thought we are comparing the same numbers:
std::cout << (1 && 2) << "\n"; // logical AND outputs 1, because both 1 and 2 represent true
std::cout << (1 & 2) << "\n"; // bitwise AND outputs 0, because it compares 0b01 with 0b10
This also applies to the ORs.

beetmacol
Автор

Wow, tihs guy knows exactly what I need, this vid is the best could happen in this period of time

pouria