Тёмный

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

The Cherno
Подписаться 667 тыс.
Просмотров 72 тыс.
50% 1

Опубликовано:

 

25 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 171   
@TheCherno
@TheCherno 3 года назад
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
@retrooper
@retrooper 3 года назад
love u cherno
@zxuiji
@zxuiji 3 года назад
you forgot the rotation operation, granted a macro is normally used but still helpful on occasion
@ripudamansingh3971
@ripudamansingh3971 3 года назад
Hey Cherno, Great Video... Well I have to ask, which tab are you using??
@KingTMK
@KingTMK Год назад
There, I hit the like button in "the description below"
@anixias
@anixias 3 года назад
I did it. I finally watched every single video in the C++ playlist. I'm gonna go write the universe now.
@aidengaede2718
@aidengaede2718 3 года назад
Does it really teach c++ well? I’m like 6 videos in right now
@anixias
@anixias 3 года назад
@@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!
@Terracraft321
@Terracraft321 3 года назад
@@aidengaede2718 yep
@Terracraft321
@Terracraft321 3 года назад
@@anixias writing it down and putting the concepts into your own words helps.
@Terracraft321
@Terracraft321 3 года назад
@@anixias what should we watch next?
@Netrole
@Netrole 3 года назад
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
@ididauni166
@ididauni166 3 года назад
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.
@cerulity32k
@cerulity32k 2 года назад
Thats exactly what I thought of when I saw that!
@raonyreis13
@raonyreis13 Год назад
woow, it blew my mind
@wowyomad
@wowyomad 8 месяцев назад
Indeed😂
@parikshitdubey3843
@parikshitdubey3843 3 года назад
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.
@kavindaravishan7351
@kavindaravishan7351 3 года назад
ha ha "don't go ahead and actually do this, please don't ", "maybe it will be fast" lol. That remind me Deadpool movie....
@kuroakevizago
@kuroakevizago 3 года назад
True it just like, the alter Cherno telling us to do so XD
@jeremygong4190
@jeremygong4190 2 года назад
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!
@alexandrelipp6666
@alexandrelipp6666 3 года назад
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; }
@UNMEASURED100
@UNMEASURED100 2 года назад
What’s wrong to use "using namespace std" instead of using std:: everywhere?
@Undefined_Variable
@Undefined_Variable 2 года назад
@@UNMEASURED100 standart library namespace is too big. #include is copy destination file and paste it to our file. So this is unnecessary.
@paintfortauva
@paintfortauva 3 года назад
I am so glad you are doing more C++ videos. Exactly what I was looking for!
@michaplucinski142
@michaplucinski142 Год назад
"...maybe it will be faster" Love those jokes
@JMRC
@JMRC 3 года назад
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
@hail2m15
@hail2m15 Год назад
Thanks, this is such a good example :)
@TheNoblegaz
@TheNoblegaz Год назад
😊 Thank you very much. This is a very good example. It solidified my understanding 👍
@soniablanche5672
@soniablanche5672 9 месяцев назад
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.
@dimi476
@dimi476 Год назад
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
@neutral_positron
@neutral_positron 10 месяцев назад
I can't find it too
@beetmacol2126
@beetmacol2126 3 года назад
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
@sukratiprasad2936
@sukratiprasad2936 5 месяцев назад
Please upload the final video soon don't leave us hanging
@Akniy
@Akniy 2 года назад
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
@victorajayi9056
@victorajayi9056 2 года назад
Eagerly waiting for the next video with real-world applications!
@griffinvonkswalgoperson9499
@griffinvonkswalgoperson9499 3 месяца назад
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.
@dadlord689
@dadlord689 Год назад
After few videos I was no longer hoping to understand this thing, but now I am. Thanks.
@bb_blocks_yt9910
@bb_blocks_yt9910 7 месяцев назад
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.
@rendoirs
@rendoirs 3 года назад
Hey Cherno :) We'd love a mutex (and locking in general) video to complement your thread video
@TapanKumar-vo7le
@TapanKumar-vo7le 3 года назад
Hey Cherno, can you do a mini series on C++ design patterns apart from the singleton one? Would be really helpful.
@TheMR-777
@TheMR-777 3 года назад
Dear *@Cherno* Many of us are waiting for the continuation of this C++ series :)
@regbot4432
@regbot4432 3 года назад
19:18, "sometimes it is faster then just setting variable to zero".. ..""but please dont do that".. (whispering) "maybe it will be faster >:)
@XeZrunner
@XeZrunner 3 года назад
Very well presented, understood all of it without questions. Thank you!
@pouria2728
@pouria2728 3 года назад
Wow, tihs guy knows exactly what I need, this vid is the best could happen in this period of time
@dimitri929
@dimitri929 3 года назад
Now that you're drawing, these episodes remind me of OneLoneCoder
@lnx648
@lnx648 3 года назад
With dark mode
@aryanparekh9314
@aryanparekh9314 3 года назад
These operators are extremely useful.
@Schytheron
@Schytheron 3 года назад
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!
@mmaranta785
@mmaranta785 3 года назад
You use these more in the embedded programming world
@Schytheron
@Schytheron 3 года назад
@@mmaranta785 I found it in the source code of a game engine (Unreal Engine 4).
@gnawlix
@gnawlix 2 года назад
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.
@pastasawce
@pastasawce 3 года назад
I really appreciate the content you put out, and really surprised you don't have a ton more likes and followers
@dogman_2748
@dogman_2748 3 года назад
Always set things to zero by xor'ing, got it!
@ahmadhadwan
@ahmadhadwan 3 года назад
the funny thing is that in assembly, most ppl zero the value of a register by xoring it by itself
@mohammadalaaelghamry8010
@mohammadalaaelghamry8010 Год назад
Great video, Thank you. I am getting huge benefit from these.
@slavago_1
@slavago_1 3 года назад
Me: Do I predict assembly language series? Cross-platform С: No, you don't.
@lnx648
@lnx648 3 года назад
@Aaron Speedy just started playing with 6502 ASM, a ton of fun actually, x86... I suspect not.
@voxelltech
@voxelltech 3 года назад
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!!!
@hhdev
@hhdev 3 года назад
Looking forward to the final video in this series. Exciting stuff.
@krupt5995
@krupt5995 3 года назад
I hope there won't be
@besusbb
@besusbb 3 года назад
4 seconds ago! never got the timing this perfect
@267praveen
@267praveen 3 года назад
And still you managed to waste this opportunity with a useless comment
@lilakouparouko1832
@lilakouparouko1832 3 года назад
17min for me
@cobblebrick
@cobblebrick 3 года назад
@@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
@codeforsteamchannel1761
@codeforsteamchannel1761 3 года назад
we need more
@emanuelsarbu7070
@emanuelsarbu7070 3 года назад
Alright, finally finished the series :) I ended up making 290 flashcards, I'm glad I did. Thank you.
@kktt1111
@kktt1111 3 года назад
You can use XOR to swap the values of variables like this: a^=b; b^=a; a^=b;
@ThisRandomUsername
@ThisRandomUsername 3 года назад
That blew my mind.
@TheZenytram
@TheZenytram 3 года назад
wow this is clever.
@MrCri1tical
@MrCri1tical 3 года назад
You are a legend for real. Next video Vtable pleaseeeeeeee
@georgerabus9314
@georgerabus9314 3 года назад
Thanks i needed this i was really confused on this topic
@justcode5626
@justcode5626 3 года назад
Thank you, Cherno. Your's video are awesome!
@SankoshSaha_01
@SankoshSaha_01 3 года назад
Finally! Waiting for this for a long time
@watermelonbig7960
@watermelonbig7960 3 года назад
I like Cherno ,your are great Software engineer
@VasaMusic438
@VasaMusic438 3 года назад
always great videos from you!!!!
@267praveen
@267praveen 3 года назад
Need these please 1. SFINAE 2. Regex 3. C++ memory model 4. CRTP 5. Variadic templates 6. Map and folds
@ahmadalastal5303
@ahmadalastal5303 3 года назад
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
@267praveen
@267praveen 3 года назад
@@ahmadalastal5303 thanks buddy
@jakubb4784
@jakubb4784 2 года назад
Where is the third episode of that mini-series? I can't find it ...
@paulobueno1
@paulobueno1 3 года назад
Two suggestions for videos: constexpr and variadic templates.
@citizenjags1309
@citizenjags1309 9 месяцев назад
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
@teksatan4699
@teksatan4699 2 года назад
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
@anoopsrana
@anoopsrana 3 года назад
If life is a bug => love is the fix
@kevinkkirimii
@kevinkkirimii 3 года назад
Bitshifting is a pain
@neutral_positron
@neutral_positron 10 месяцев назад
Am I the one one or the 3rd video isn't to be found on the internet
@aashishsingla2499
@aashishsingla2499 2 года назад
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.
@RozeFound
@RozeFound 3 года назад
How about introducing to concepts in C++?
@georgerabus9314
@georgerabus9314 3 года назад
i love how you put #sponsored word in the corner so i know where i can stop skipping
@ramesses_ii
@ramesses_ii 3 года назад
Shhh. Leaving comments like this can make sponsors unhappy with him. Who knows if they read the comments?
@georgerabus9314
@georgerabus9314 3 года назад
@@ramesses_ii :))
@YoloMonstaaa
@YoloMonstaaa 3 года назад
the logical next step is timestamps
@pseudounknow5559
@pseudounknow5559 3 года назад
A video about mutex or multithreading in details would be very cool ^^
@phillyphill5228
@phillyphill5228 2 года назад
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!
@mr.anderson5077
@mr.anderson5077 3 года назад
Yeah, Charno again!!!
@immalemonnyyy7967
@immalemonnyyy7967 3 года назад
I was literally in the middle of building logic gates like these and then this video jumped out
@samitkapoor53
@samitkapoor53 3 года назад
quality content!
@LostArkLover
@LostArkLover 3 года назад
I wanted this!!! thanks
@nac9880
@nac9880 3 года назад
I wonder what the shift left/right operators look like
@computerprogrammer7942
@computerprogrammer7942 3 года назад
Cherno just wondering do you know anymore programming languages or is it just C++
@slobodanstajic652
@slobodanstajic652 2 года назад
Cherno, can you do some more videos on templates?
@lolipopjojo6218
@lolipopjojo6218 3 года назад
Currently using it to make a simple app for addressing and subneting ipv4 adresses
@westernvibes1267
@westernvibes1267 3 года назад
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
@computerprogrammer7942
@computerprogrammer7942 3 года назад
People hate unreal engine C++ that’s why lol
@computerprogrammer7942
@computerprogrammer7942 3 года назад
You normally have to buy courses which are the good ones
@computerprogrammer7942
@computerprogrammer7942 3 года назад
I don’t think he knows unreal engine
@lolipopjojo6218
@lolipopjojo6218 3 года назад
Ye would be great even just for an intro to get started with UE
@computerprogrammer7942
@computerprogrammer7942 3 года назад
@@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
@Goejii
@Goejii 3 года назад
- Repeat after me : X - X - OR - OR - XOR - Zooor!
@psychotrout
@psychotrout 3 года назад
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!
@furtalance_x
@furtalance_x 3 года назад
Brother nothing on 'more cherno' channel?!!!
@stepanhrbek8151
@stepanhrbek8151 3 года назад
Here we goooo
@agustinmarzioni5216
@agustinmarzioni5216 Год назад
i love you thanks
@cmdlp4178
@cmdlp4178 3 года назад
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
@enderfulgur9480
@enderfulgur9480 3 года назад
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
@rafalmichalski4893
@rafalmichalski4893 3 года назад
Hello Cherno which graphical tablet do you use ?
@hishamsomroz6913
@hishamsomroz6913 3 года назад
ohh wow that's great.
@bpeachey1475
@bpeachey1475 3 года назад
Hey guys, STL algorithms library and c++ 'fluency'... What's your opinions??
@modo4211
@modo4211 2 года назад
Please make a Video about Concepts in C++20
@devinschlegel1763
@devinschlegel1763 3 года назад
when will you make a full tutorial for multi threading?
@nickromano3391
@nickromano3391 3 года назад
Can you create a video on making Linked Lists and Hash Tables in C++?
@GenericInternetter
@GenericInternetter 3 года назад
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.
@xTriplexS
@xTriplexS 11 месяцев назад
You need cmath header file for std::pow(number, power) which is what we use for exponents
@AviPars
@AviPars 2 года назад
Is there a shift left arithmetic in addition to logical?
@anonymoususer7663
@anonymoususer7663 3 года назад
I'm totally !Getting it 😅👍
@abhishekgaurav6614
@abhishekgaurav6614 3 года назад
Liked first and then watching ☝
@SPL1NTER_SE
@SPL1NTER_SE 3 года назад
Should have mentioned alternatingly toggling bits using XOR.
@acbytes
@acbytes 3 года назад
Hi there! Could you create a video on C++20 Concepts and Constraints? Thanks!
@nathans_codes
@nathans_codes 2 года назад
where ep 3
@YourMaster000
@YourMaster000 3 года назад
Please try to add Java support in hazel
@neuro5261
@neuro5261 3 года назад
Can you cover c++20 module
@richardj.rustleshyman2026
@richardj.rustleshyman2026 3 года назад
Can you do a video on std::bitset?
@logiclegend8219
@logiclegend8219 3 года назад
Anyone knows whether this series goes into advanced c++ or not?
@rcookman
@rcookman 3 года назад
The Cherno uses a 9 bit computer! Oh no.
@ehsan1147
@ehsan1147 3 года назад
guys does this course worth watching?
@KuroiMeansBlack
@KuroiMeansBlack 21 день назад
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
@shahriardhruvo4333
@shahriardhruvo4333 2 года назад
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...
@RealityCheck6969
@RealityCheck6969 Год назад
I am watching the video the second time and I am still desperate... :/
@silvertakana3932
@silvertakana3932 3 года назад
i can't find the like button in the description below
@yusufefl
@yusufefl 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
@qwertyytrewq2963
@qwertyytrewq2963 3 года назад
can someone tell me why ~5 always result to -6 ;-;
@guilhermealveslopes
@guilhermealveslopes 3 года назад
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
@guilhermealveslopes
@guilhermealveslopes 3 года назад
Much unlike php, which only returns Booleans when using logical operators...
@kanony5188
@kanony5188 3 года назад
Talk about SFINAE
Далее
I did a C++ University Assignment
50:23
Просмотров 295 тыс.
Intro to Binary and Bitwise Operators in C++
21:40
Просмотров 136 тыс.
Гаджет из даркнета 📦
00:45
Просмотров 140 тыс.
Bitwise Operators and WHY we use them
8:41
Просмотров 85 тыс.
lvalues and rvalues in C++
14:13
Просмотров 320 тыс.
why are switch statements so HECKIN fast?
11:03
Просмотров 421 тыс.
Master Pointers in C:  10X Your C Coding!
14:12
Просмотров 318 тыс.
WHY did this C++ code FAIL?
38:10
Просмотров 267 тыс.
Macros in C++
19:36
Просмотров 239 тыс.
31 nooby C++ habits you need to ditch
16:18
Просмотров 806 тыс.
ARRAYLIST VS LINKEDLIST
21:20
Просмотров 69 тыс.
Stop using std::vector wrong
23:14
Просмотров 127 тыс.