Thank you all for watching! Excited to finish this mini-series in the next episode. Don't forget that the first 1000 people to use the link will get a free trial of Skillshare Premium Membership: skl.sh/thecherno04211
@@aidengaede2718 You HAVE to use the knowledge you gain from the videos if you want to learn anything and improve. You can't just passively absorb his content and expect to get good. If you do small projects using his videos as a prompt, then yes, I'd say they're very good!
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
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.
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) (11111111 00000000 00000000 11111111) (4278190335) - Fully opaque white: (255, 255, 255, 255) (11111111 11111111 11111111 11111111) (4294967295) - Fully opaque black: (0, 0, 0, 255) (00000000 00000000 00000000 11111111) (255) - Transparent yellow: (0, 255, 255, 128) (00000000 11111111 11111111 10000000) (4278190335) - Invisible blue: (0, 255, 255, 0) (00000000 11111111 11111111 00000000) (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 (11111111) to the green channel 16 bits to the left: 00000000 11111111 00000000 00000000 (16711680) - We use '&', which will result in only the green channel remaining: 00000000 01001110 00000000 00000000 (5111808) - We shift with '>>' the green values to the beginning: 00000000 00000000 00000000 01001110 (78) The result is 78 Let's implement it. #include // Let's avoid magic numbers. #define RED_OFFSET 32; #define RED_MASK (255
good example but it's way more simple to simply use an array of 4 chars, you accomplish the same thing but much easier lol. And you can even cast it to a uint32_t later if you want with type puning.
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& a, const std::vector& b) { int elem = 0; for (const int& num : a) { elem ^= num; } for (const int& num : b) { elem ^= num; } return elem; }
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!
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
Did you ever get around to do the 3rd video about bitwise operators? I'd love to see some actual code examples with clever uses of these guys. Thx for your C++ content, it's very much appreciated
I kind of think of XOR as a way to flip just one specific bit. In all the cases where the second value was 0 in the table you drew, the first value didn’t change in the end value. In all the cases where the second value was 1, the first value did change. I used that about a week or two ago to flip a specific bit in a thing I’m making, which changes the graphic for a block it draws based on whether there are other blocks next to it. If there was a block above, I would flip the first bit since that was the bit I decided corresponded to up, and so on with other directions. That way I could represent each sort of block orientation with a 4-digit binary number.
I think its funny when you say to try this out on paper. I am watching this video purely as a refresher since i already learned all this through Minecraft Redstone. I made a cool timer that you can pause, add to hours/minutes, and subtract from hours/minutes.
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.
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!
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!!!
Bit shifting is mostly for hardware guys. Its to set configurations or access chipsets, all sorts of things, my hardware code has bit shifting galore..lol .. specially the microcontrollers code
oneproduct Channel is doing a great job discussing templates, SFINAE, variadic templates, and for memory model check cpp conference they do a great job discussing them, I recommend you threading/vectorization, move semantics, perfect forwarding and memory management
To practice this video a bit, i've made a little class that contains 8 bools in 1 byte. (Also this is probabely slower than using real bools even if it's much cheaper for the ram so don't take this as a real usable class) : class Bools8 { private : char m_bools = 0; // the actual list of bools public : // note that the constructor below "reverse" the bool "list" (so if u call Bools8(1, 0, 1, 0, 0), m_bools will be (in binary) 00000101) Bools8(bool first = 0, bool second = 0, bool third = 0, bool fourth = 0, bool fifth = 0, bool sixth = 0, bool seventh = 0, bool eight = 0) : m_bools(first | second
I thought you would discuss use bitwise operators for setting flags in c++. Which I think might be one of the only practical uses where bitwise operators are needed.
Am I the only one who is using binary numbers with those operators as sets and vectors? On the left I write the mathematical notation, on the right the C++ code. You can implement a set for at max 64 elements with an integer (In python the limit is endless, because of the bigint). Let X be all the elements. A and B are subsets: Ø = 0 X = ~0 = -1 A n B = A & B A u B = A | B A \ B = A & ~B X \ A = ~B = X & X ^ B = X ^ B ** A /\ B = A ^ B // /\ Symmetric difference |A| = popcount(A) ** in Risc V there is no not instruction and xor with -1 might be used instead. You can use binary numbers as Bitvectors or Bitarrays: u, v in {0, 1}^64 u (*) v = u & v // (*) Hadamard product u * v = bitcount(a & b) // * Dot product u + v = u - v = u ^ v // +/- wrapping vector addition/difference, with the special case 1 + 1 = 0 for each component u [+] v = u | v // [+] saturating addition, with special case 1 [+] 1 = 1 And remember that polynomial arithmetic over {0, 1} can also be implemented with binary operators, this is used for checksums There are some important bit functions missing in C++, that I always use: - popcount -- number of set bits in the int - clz/ctz -- count leading/trailing zeros
Have you ever thought about making a series on unreal c++? Would really love to see your ue4 skills and there are not even enough resources for peoples to get started
Hey @The Cherno! You are bad ass at making these tuts and stuff my man.. In saying that I have a request! that involves what your doing here a little. Could you maybe make a tut on how to encode/decode websocket frames? They require bit manipulation to parse the payload(message) and get a masking key, op code, and payload length(the actual length of the data minus the header etc. I've seen pretty much no tuts anywhere.. on how to do it. everyone is using libs (which most are horrible, and won't actually work outside of the libs own internal networking code.), and don't show in anyway how to actually process the payload frames.(encoding/decoding them). Tons of people asking about it, but the only options we seem to have is to use a bulky over bloated library..(that doesn't work half the time for our apps actual needs) or not use websockets at all, and that's just a shame. for the average programmer
Not much to tell this time, maybe there some work to do writing by hand '&' operator but I enjoyed the video and the information were on point. Thank you!
12:10 could you just add the 0's so that you don't have to shift? I'm assuming you can, so there must be a reason that you aren't doing that? Could someone please explain? Thank you!
You know what I really love? Logical operator short circuiting and returning the operand.. Like in js ("Hello" && var) || 1000 That will return the value of var if the string "Hello" is true (and it is), else, it returns 1000 :D
Can anyone give me the link of his next video about this topic where he discussed about the real world examples where to use these bitwise operators? He already mentioned in this video that he will make another one on this topic discussing about the use case of bitwise operator...
How does the compiler know that ^ means XOR instead of raising to an exponent? In the previous video you even wrote "2^n" in the context of exponents. In this video you wrote "a^a" which would be interpreted as raising a to exponent a.
I am late ik, but I started programming last year and i can definetely Say that Cherno Is the best teacher that explain c++ world Wide, and so yes it's worth watching him
"Is this true and is this true?" and the answer is "YES". So YES = TRUE. Irrelevant question: What kind of operations do you do a "bit wisely"? Meme answer: -YES
Thank you all for watching! Excited to finish this mini-series in the next episode. Don't forget that the first 1000 people to use the link will get a free trial of Skillshare Premium Membership: skl.sh/thecherno04211
love u cherno
you forgot the rotation operation, granted a macro is normally used but still helpful on occasion
Hey Cherno, Great Video... Well I have to ask, which tab are you using??
There, I hit the like button in "the description below"
I did it. I finally watched every single video in the C++ playlist. I'm gonna go write the universe now.
Does it really teach c++ well? I’m like 6 videos in right now
@@aidengaede2718 You HAVE to use the knowledge you gain from the videos if you want to learn anything and improve. You can't just passively absorb his content and expect to get good. If you do small projects using his videos as a prompt, then yes, I'd say they're very good!
@@aidengaede2718 yep
@@anixias writing it down and putting the concepts into your own words helps.
@@anixias what should we watch next?
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
Please upload the final video soon don't leave us hanging
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.
Thats exactly what I thought of when I saw that!
woow, it blew my mind
Indeed😂
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) (11111111 00000000 00000000 11111111) (4278190335)
- Fully opaque white: (255, 255, 255, 255) (11111111 11111111 11111111 11111111) (4294967295)
- Fully opaque black: (0, 0, 0, 255) (00000000 00000000 00000000 11111111) (255)
- Transparent yellow: (0, 255, 255, 128) (00000000 11111111 11111111 10000000) (4278190335)
- Invisible blue: (0, 255, 255, 0) (00000000 11111111 11111111 00000000) (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 (11111111) to the green channel 16 bits to the left: 00000000 11111111 00000000 00000000 (16711680)
- We use '&', which will result in only the green channel remaining: 00000000 01001110 00000000 00000000 (5111808)
- We shift with '>>' the green values to the beginning: 00000000 00000000 00000000 01001110 (78)
The result is 78
Let's implement it.
#include
// Let's avoid magic numbers.
#define RED_OFFSET 32;
#define RED_MASK (255
Thanks, this is such a good example :)
😊 Thank you very much. This is a very good example. It solidified my understanding 👍
good example but it's way more simple to simply use an array of 4 chars, you accomplish the same thing but much easier lol. And you can even cast it to a uint32_t later if you want with type puning.
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& a, const std::vector& b) {
int elem = 0;
for (const int& num : a) {
elem ^= num;
}
for (const int& num : b) {
elem ^= num;
}
return elem;
}
What’s wrong to use "using namespace std" instead of using std:: everywhere?
@@UNMEASURED100 standart library namespace is too big. #include is copy destination file and paste it to our file. So this is unnecessary.
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.
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!
ha ha "don't go ahead and actually do this, please don't ", "maybe it will be fast" lol.
That remind me Deadpool movie....
True it just like, the alter Cherno telling us to do so XD
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
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
I am so glad you are doing more C++ videos. Exactly what I was looking for!
"...maybe it will be faster"
Love those jokes
Did you ever get around to do the 3rd video about bitwise operators? I'd love to see some actual code examples with clever uses of these guys. Thx for your C++ content, it's very much appreciated
I can't find it too
I kind of think of XOR as a way to flip just one specific bit. In all the cases where the second value was 0 in the table you drew, the first value didn’t change in the end value. In all the cases where the second value was 1, the first value did change. I used that about a week or two ago to flip a specific bit in a thing I’m making, which changes the graphic for a block it draws based on whether there are other blocks next to it. If there was a block above, I would flip the first bit since that was the bit I decided corresponded to up, and so on with other directions. That way I could represent each sort of block orientation with a 4-digit binary number.
I think its funny when you say to try this out on paper. I am watching this video purely as a refresher since i already learned all this through Minecraft Redstone. I made a cool timer that you can pause, add to hours/minutes, and subtract from hours/minutes.
Hey Cherno, can you do a mini series on C++ design patterns apart from the singleton one? Would be really helpful.
Dear *@Cherno* Many of us are waiting for the continuation of this C++ series :)
19:18, "sometimes it is faster then just setting variable to zero".. ..""but please dont do that".. (whispering) "maybe it will be faster >:)
Hey Cherno :)
We'd love a mutex (and locking in general) video to complement your thread video
After few videos I was no longer hoping to understand this thing, but now I am. Thanks.
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.
Eagerly waiting for the next video with real-world applications!
Now that you're drawing, these episodes remind me of OneLoneCoder
With dark mode
Very well presented, understood all of it without questions. Thank you!
Wow, tihs guy knows exactly what I need, this vid is the best could happen in this period of time
You can use XOR to swap the values of variables like this:
a^=b;
b^=a;
a^=b;
That blew my mind.
wow this is clever.
Always set things to zero by xor'ing, got it!
the funny thing is that in assembly, most ppl zero the value of a register by xoring it by itself
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!
You use these more in the embedded programming world
@@mmaranta785 I found it in the source code of a game engine (Unreal Engine 4).
Alright, finally finished the series :)
I ended up making 290 flashcards, I'm glad I did.
Thank you.
These operators are extremely useful.
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!!!
Great video, Thank you. I am getting huge benefit from these.
Bit shifting is mostly for hardware guys. Its to set configurations or access chipsets, all sorts of things, my hardware code has bit shifting galore..lol .. specially the microcontrollers code
I really appreciate the content you put out, and really surprised you don't have a ton more likes and followers
Need these please
1. SFINAE
2. Regex
3. C++ memory model
4. CRTP
5. Variadic templates
6. Map and folds
oneproduct Channel is doing a great job discussing templates, SFINAE, variadic templates, and for memory model check cpp conference they do a great job discussing them, I recommend you threading/vectorization, move semantics, perfect forwarding and memory management
@@ahmadalastal5303 thanks buddy
Where is the third episode of that mini-series? I can't find it ...
Am I the one one or the 3rd video isn't to be found on the internet
Looking forward to the final video in this series. Exciting stuff.
I hope there won't be
Two suggestions for videos: constexpr and variadic templates.
To practice this video a bit, i've made a little class that contains 8 bools in 1 byte. (Also this is probabely slower than using real bools even if it's much cheaper for the ram so don't take this as a real usable class) :
class Bools8
{
private :
char m_bools = 0; // the actual list of bools
public :
// note that the constructor below "reverse" the bool "list" (so if u call Bools8(1, 0, 1, 0, 0), m_bools will be (in binary) 00000101)
Bools8(bool first = 0, bool second = 0, bool third = 0, bool fourth = 0, bool fifth = 0, bool sixth = 0, bool seventh = 0, bool eight = 0)
: m_bools(first | second
You are a legend for real. Next video Vtable pleaseeeeeeee
Thanks i needed this
i was really confused on this topic
we need more
I thought you would discuss use bitwise operators for setting flags in c++. Which I think might be one of the only practical uses where bitwise operators are needed.
4 seconds ago! never got the timing this perfect
And still you managed to waste this opportunity with a useless comment
17min for me
@@267praveen Waste what opportunity? Typing out 10 words when something very improbable happens isn't half as bad as you make it out to be
Me: Do I predict assembly language series?
Cross-platform С: No, you don't.
@Aaron Speedy just started playing with 6502 ASM, a ton of fun actually, x86... I suspect not.
I was literally in the middle of building logic gates like these and then this video jumped out
If life is a bug => love is the fix
Thank you, Cherno. Your's video are awesome!
Bitshifting is a pain
Finally! Waiting for this for a long time
I like Cherno ,your are great Software engineer
Am I the only one who is using binary numbers with those operators as sets and vectors?
On the left I write the mathematical notation, on the right the C++ code.
You can implement a set for at max 64 elements with an integer (In python the limit is endless, because of the bigint). Let X be all the elements. A and B are subsets:
Ø = 0
X = ~0 = -1
A n B = A & B
A u B = A | B
A \ B = A & ~B
X \ A = ~B = X & X ^ B = X ^ B **
A /\ B = A ^ B // /\ Symmetric difference
|A| = popcount(A)
** in Risc V there is no not instruction and xor with -1 might be used instead.
You can use binary numbers as Bitvectors or Bitarrays: u, v in {0, 1}^64
u (*) v = u & v // (*) Hadamard product
u * v = bitcount(a & b) // * Dot product
u + v = u - v = u ^ v // +/- wrapping vector addition/difference, with the special case 1 + 1 = 0 for each component
u [+] v = u | v // [+] saturating addition, with special case 1 [+] 1 = 1
And remember that polynomial arithmetic over {0, 1} can also be implemented with binary operators, this is used for checksums
There are some important bit functions missing in C++, that I always use:
- popcount -- number of set bits in the int
- clz/ctz -- count leading/trailing zeros
A video about mutex or multithreading in details would be very cool ^^
Have you ever thought about making a series on unreal c++? Would really love to see your ue4 skills and there are not even enough resources for peoples to get started
People hate unreal engine C++ that’s why lol
You normally have to buy courses which are the good ones
I don’t think he knows unreal engine
Ye would be great even just for an intro to get started with UE
@@lolipopjojo6218 there is no point of him doing that the whole point of his videos is to learn how to make a game engine not use a game engine
How about introducing to concepts in C++?
i love how you put #sponsored word in the corner so i know where i can stop skipping
Shhh. Leaving comments like this can make sponsors unhappy with him. Who knows if they read the comments?
@@ramesses_ii :))
the logical next step is timestamps
Hey @The Cherno! You are bad ass at making these tuts and stuff my man.. In saying that I have a request! that involves what your doing here a little. Could you maybe make a tut on how to encode/decode websocket frames? They require bit manipulation to parse the payload(message) and get a masking key, op code, and payload length(the actual length of the data minus the header etc. I've seen pretty much no tuts anywhere.. on how to do it. everyone is using libs (which most are horrible, and won't actually work outside of the libs own internal networking code.), and don't show in anyway how to actually process the payload frames.(encoding/decoding them). Tons of people asking about it, but the only options we seem to have is to use a bulky over bloated library..(that doesn't work half the time for our apps actual needs) or not use websockets at all, and that's just a shame. for the average programmer
Currently using it to make a simple app for addressing and subneting ipv4 adresses
always great videos from you!!!!
I wonder what the shift left/right operators look like
Not much to tell this time, maybe there some work to do writing by hand '&' operator but I enjoyed the video and the information were on point. Thank you!
Cherno, can you do some more videos on templates?
Yeah, Charno again!!!
Hey guys, STL algorithms library and c++ 'fluency'... What's your opinions??
Cherno just wondering do you know anymore programming languages or is it just C++
Should have mentioned alternatingly toggling bits using XOR.
Hello Cherno which graphical tablet do you use ?
Can you create a video on making Linked Lists and Hash Tables in C++?
Here we goooo
ohh wow that's great.
12:10 could you just add the 0's so that you don't have to shift? I'm assuming you can, so there must be a reason that you aren't doing that? Could someone please explain? Thank you!
quality content!
I wanted this!!! thanks
Please make a Video about Concepts in C++20
You know what I really love? Logical operator short circuiting and returning the operand..
Like in js
("Hello" && var) || 1000
That will return the value of var if the string "Hello" is true (and it is), else, it returns 1000 :D
Much unlike php, which only returns Booleans when using logical operators...
when will you make a full tutorial for multi threading?
I am watching the video the second time and I am still desperate... :/
i love you
thanks
- Repeat after me : X
- X
- OR
- OR
- XOR
- Zooor!
Brother nothing on 'more cherno' channel?!!!
Hi there! Could you create a video on C++20 Concepts and Constraints? Thanks!
Liked first and then watching ☝
Anyone knows whether this series goes into advanced c++ or not?
Can anyone give me the link of his next video about this topic where he discussed about the real world examples where to use these bitwise operators? He already mentioned in this video that he will make another one on this topic discussing about the use case of bitwise operator...
Please try to add Java support in hazel
Can you do a video on std::bitset?
I'm totally !Getting it 😅👍
Is there a shift left arithmetic in addition to logical?
Can you cover c++20 module
How does the compiler know that ^ means XOR instead of raising to an exponent?
In the previous video you even wrote "2^n" in the context of exponents.
In this video you wrote "a^a" which would be interpreted as raising a to exponent a.
You need cmath header file for std::pow(number, power) which is what we use for exponents
guys does this course worth watching?
I am late ik, but I started programming last year and i can definetely Say that Cherno Is the best teacher that explain c++ world Wide, and so yes it's worth watching him
where ep 3
"Is this true and is this true?" and the answer is "YES". So YES = TRUE. Irrelevant question: What kind of operations do you do a "bit wisely"? Meme answer: -YES
not(~) is just ^-1
i can't find the like button in the description below
The Cherno uses a 9 bit computer! Oh no.
can someone tell me why ~5 always result to -6 ;-;