Practical uses of bitwise operations - Implementing a Flag System

  Рет қаралды 38,712

CodeVault

CodeVault

Күн бұрын

Пікірлер: 87
@joaoestudante1897
@joaoestudante1897 2 жыл бұрын
Thanks for the great explanation!! If i'm not mistaken you can also do a "flag toggler" with a xor operator between the flag variable and the flag to toogle: 10000011 10000001 00000010 ^ 00000010^ 10000001 10000011
@CodeVault
@CodeVault 2 жыл бұрын
Yes, that is correct. I may have forgotten to mention that. Good catch!
@jameseze9469
@jameseze9469 Жыл бұрын
This is the most interesting tutorial I've ever watched since I was in my mother's womb. I love you...
@drtalon1668
@drtalon1668 Жыл бұрын
Having studied at least 10-15 different explanations around the subject of bitwise operations, none really provided the "lightbulb" moment. Sure, all of those other videos explained what the operations did, and I was able to understand them, but this was by far the best example of what bitwise operations are actually useful for. After watching, I was able to come to this understanding on my own: Manipulation or assessment techniques of individual bits within an allocated memory space; used to efficiently represent states, properties or attributes of a system with the smallest allocation of memory required to do so.
@DONJAJO
@DONJAJO Жыл бұрын
Thank you for this. After years of programming, I've found myself implementing a websocket server specifications that require bit masking. Your video has helped greatly in understanding bitwise operations. Thank you.
@flaviocampos3581
@flaviocampos3581 3 жыл бұрын
I am learning how to send bit from client, server using signals. So I came to look for the bits thing that I had no idea how it works and this is so cool. Now I want to learn more. Thanks!
@corpseopera
@corpseopera 2 жыл бұрын
minitalk
@judeleon8485
@judeleon8485 3 жыл бұрын
Wow! There is no better explanation anywhere else. So well explained, thanks
@abiiranathan
@abiiranathan 2 жыл бұрын
This is mind-blowing. Guy explains things like a professor. No buts!
@invorokner282
@invorokner282 3 жыл бұрын
this channel it pure gold. I'm programming something for my company and this is so useful. generally this is gold.
@firwanaa5526
@firwanaa5526 Жыл бұрын
Your tutorial is truly elegant and informative. The concise video taught me a lot and I appreciate how this channel is full of hidden gems that help demystify complex concepts. As I am currently studying 'Computer Systems: A Programmer's Perspective', your work has been incredibly helpful. Thank you for providing such amazing content. I would be thrilled to see more on system and network programming. If you have any particular book or topics in mind, please let me know as I will certainly follow. Thanks again for your great work!
@ninjaduck3534
@ninjaduck3534 3 жыл бұрын
Thank you immensely! It was very helpful to see the practical use of bitwise operations and not just how to do them.
@muumitramm
@muumitramm 2 жыл бұрын
Good explanation. You can improve maintainability with left bitshift: #define FIRST_FLAG 1
@CodeVault
@CodeVault 2 жыл бұрын
Didn't think of that. That's a nice way to go about it
@tombrady8861
@tombrady8861 2 жыл бұрын
After 20 years of learning this I finally understand the usecase .
@mynameisearlb
@mynameisearlb 3 жыл бұрын
Thank you for this! So the benefit of utilizing bitwise operations is to be able to drastically reduce the amount of required memory in your programs. I originally started learning C maybe 9 months ago and decided to switch to Python. Then a couple weeks ago I discovered that it's much better to make Android applications with java instead of python. Now I've just came to the bitwise portion of my Java book and they don't describe why these would be useful. I seen some bitwise operations being used in some source code in C related to the pinephone and googled for a while and couldn't figure out what was going on. With the help of this video, now I can at least grasp the concept. So now I get it, at least somewhat Great video!
@muumitramm
@muumitramm 2 жыл бұрын
20:16 database example. Not only memory. You could store the bitflags into database integer field (instead of 32 separate boolean fields) which saves some space on disk too.
@vfryhn2
@vfryhn2 2 жыл бұрын
I'm starting with low level programming, and this is an awesome explanation, thank you for sharing your knowledge!
@geraldikenna6444
@geraldikenna6444 2 жыл бұрын
A true Engineer you are the best. Thank you for the explanation. It was very helpful
@exoticcoder5365
@exoticcoder5365 3 жыл бұрын
mind blown, at the same time very clear explanation 👍
@mavrix2356
@mavrix2356 4 жыл бұрын
We won't get on udemy what we get on your channel. Thank you so much bro
@miyamotomusashi4556
@miyamotomusashi4556 2 жыл бұрын
Honestly, I have bought courses on udemy only to discover that there were better material available free on KZbin. The only good thing that came from those courses is a well structured learning plan. I am not saying udemy courses are bad, I bought some and I have no regrets over it. I am just saying that sometimes, free resources are better!
@shubhamzingare4433
@shubhamzingare4433 2 жыл бұрын
lots of love to u. absolutely loved how you explained it by taking practical example.
@RichardLRogers
@RichardLRogers 3 жыл бұрын
Wonderful step by steps @15:23 !
@ManiasInc
@ManiasInc 11 ай бұрын
what a good explanation!! Congratulations! Thanks for sharing your knowledge!👏🏽👏🏽😁😁🏆🏆
@kitchenchem141
@kitchenchem141 9 ай бұрын
Thank you, I never knew exactly why I would need to use these
@chrisperkins1358
@chrisperkins1358 3 жыл бұрын
Thanks for the great video, clear and very well explained. After reading 2.9 of The C Programming Language, I was completely baffled. Now it makes much more sense.
@Liam-ey2gs
@Liam-ey2gs 3 жыл бұрын
I was in the same boat
@Liam-ey2gs
@Liam-ey2gs 3 жыл бұрын
Thank you so much for your effort, your channel is an invaluable resource
@sygaming1199
@sygaming1199 3 жыл бұрын
This video is pure gold.
@jasonoliver2752
@jasonoliver2752 2 жыл бұрын
Really loved this explanation! Extremely clear and easy to follow.
@akosyt4540
@akosyt4540 2 жыл бұрын
THANK YOU MAN YOU REALLY HELPED ME TO UNDER THIS TOPIC AND IMPLEMENT IT IN MY PROGRAM
@Bestcuriosity_1
@Bestcuriosity_1 3 жыл бұрын
your explanation is so clear my professor can't explain me
@brent56and1
@brent56and1 Жыл бұрын
Just found out C# has this feature too. Very nice for storing multiple enum values in a single variable.
@hilarytitus6157
@hilarytitus6157 2 жыл бұрын
This is beautiful. Thank You
@IllumTheMessage
@IllumTheMessage Жыл бұрын
Superb description.
@lisandroiaffar4501
@lisandroiaffar4501 Жыл бұрын
Thank you so much for such quality material 😁
@wasgehtabman89
@wasgehtabman89 Жыл бұрын
Sensational video! Wow!
@yelimsnusm7551
@yelimsnusm7551 2 жыл бұрын
18:30 ive been able to use bitwise operators next to the equal sign like with += or -=. it works.. maybe worth pointing out too. So you could write flags &= ~SECOND_FLAG; flags |= SECOND_FLAG;
@CodeVault
@CodeVault 2 жыл бұрын
Good point!
@muratkaradas1483
@muratkaradas1483 2 жыл бұрын
Thanks for this great video 👍
@stephenjames5745
@stephenjames5745 4 жыл бұрын
Excellent video. Thank you
@nshreesha2011
@nshreesha2011 3 жыл бұрын
Amazing explanation! Thanks!!
@shrimpfights17
@shrimpfights17 3 жыл бұрын
the youtube example really helped great work
@igmu2681
@igmu2681 3 жыл бұрын
Thanks for the good video!
@AlokSingh-se8ye
@AlokSingh-se8ye 2 жыл бұрын
Thanks for this amazing explanation!!
@HasinthaWeragala
@HasinthaWeragala Жыл бұрын
Thank you sir, very well explained
@maxallytheresias3547
@maxallytheresias3547 3 жыл бұрын
amazing explanation🔥🔥🔥
@oktayozturk5673
@oktayozturk5673 2 жыл бұрын
excellent explantion
@nullzero9224
@nullzero9224 3 жыл бұрын
Thanks - excellent explaination
@DarwinBuelo
@DarwinBuelo 3 жыл бұрын
Thank you. This is very helpful
@msmalik681
@msmalik681 3 жыл бұрын
Very helpful thank you.
@ComunidadGLLA
@ComunidadGLLA 3 жыл бұрын
So well explained! Thank you
@deli5777
@deli5777 3 жыл бұрын
very helpful and interesting, thanks!
@juaninfante7000
@juaninfante7000 4 ай бұрын
Sooooooooo Dope!!! That is soooooooo coool
@vhmvdn
@vhmvdn Жыл бұрын
Great content! I have a few questions tho. Do the macros take up memory as well? The minimum being a byte? Would is still be memory efficient with all those macros? Also, is this way of doing conditional branching more performant or relatively slower compared to a if (flag) { } I’m assuming it would be a little slow because the bits have to be manipulated first?
@CodeVault
@CodeVault Жыл бұрын
The important aspect to understand about macros is that they disappear after the preprocessor step so, everything that starts with # actually disappears before the compilation begins thus, macros never add to execution time only pre-processing time. That if statement would be a tiny bit less performant, for sure. But it's quite insignificant I think. Bitwise operations are some of the fastest executing in a CPU
@looploop6612
@looploop6612 5 жыл бұрын
thanks There is a question using bit operation: Given a number n, find length of the longest consecutive 1s in its binary representation. e.g. Input : n = 222 Output : 4 The binary representation of 222 is 11011110. The most efficient method is bitwise operation to find longest ones. But it is hard for me to understand it.
@CodeVault
@CodeVault 5 жыл бұрын
In the video I used *x & 1* to check if the last binary digit of x is 1. If you use that with the right shift operation in a loop will get you the proper result.
@dayokesola
@dayokesola 2 жыл бұрын
Purrfect!
@nitinkulkarni7942
@nitinkulkarni7942 3 жыл бұрын
Super Super example. Thank u
@y4ni608
@y4ni608 2 жыл бұрын
I love you man grat tutorial
@Fullstackdev-
@Fullstackdev- 3 жыл бұрын
thanks
@itsFojle
@itsFojle 11 ай бұрын
Brother ❤❤
@prashis
@prashis 3 жыл бұрын
Thanks man!!
@chinpokomon_
@chinpokomon_ 4 жыл бұрын
thank you
@jameseze9469
@jameseze9469 Жыл бұрын
I just have to make a double comment because this is niceeeeee
@anty.
@anty. 2 жыл бұрын
Question: Is this something a compiler automatically does when a program has a lot of bools? Or do we have to manually implement this to save the memory?
@CodeVault
@CodeVault 2 жыл бұрын
I don't think any of the compilers do such an optimization. But I'm no expert in compilers so more research is needed regarding this
@slzckboy
@slzckboy 2 жыл бұрын
you can tell a good explanation when before thr person gets to the punch line u can see where they are going already!
@its_code
@its_code Жыл бұрын
❤❤❤❤
@ChristopherPhillipsDev
@ChristopherPhillipsDev 3 жыл бұрын
I sincerely appreciate your content! I've visited multiple times and everything is so very clear and laid out well. Regarding your #definitions, is there any performance gain doing it your way vs having #define FIRST 1 #define SECOND 2 #define THIRD 4 ?
@CodeVault
@CodeVault 3 жыл бұрын
It won't affect performance when running the program. It *might* affect compile time... but just by a very small amount. So I think it's fine if you want to do it that way!
@goldhalowings
@goldhalowings 5 жыл бұрын
Thanks, i guess next video should be about bitwise shift operators
@CodeVault
@CodeVault 5 жыл бұрын
We've already uploaded a video about them: kzbin.info/www/bejne/oqfNiGCXisiVfJI
@goldhalowings
@goldhalowings 5 жыл бұрын
​@@CodeVault I followed from a certain point the tutorials that's why i didn't notice, silly of me, thanks
@marouanees-said6251
@marouanees-said6251 2 жыл бұрын
Thanks for the explanation. I have a question: How do you run your c code in cmd instead of terminal's vs code ? Thanks again.
@CodeVault
@CodeVault 2 жыл бұрын
On Linux you can simply do: gcc main.c -o main ./main For Windows you can try installing WSL or use the VisualC or other Windows compilers (like MinGW)
@msmalik681
@msmalik681 2 жыл бұрын
If i use this for a 32 bit application if i then update that application to 64 bit will everything break ?
@CodeVault
@CodeVault 2 жыл бұрын
No. It should work the same. An unsigned int should be only 4 bytes on both 32-bit and 64-bit architectures. And the rest are just bitwise operations which are never affected by the architecture (as far as I know)
@joymakerRC
@joymakerRC 2 жыл бұрын
love u
@_slier
@_slier 2 жыл бұрын
| = bit setter & = bit clearer ~ = bit toggler
@Rahulsharma-rg8ce
@Rahulsharma-rg8ce 5 ай бұрын
Flagging this video as important 😅
@Necropheliac
@Necropheliac 12 күн бұрын
I understand that programming this way would be necessary for writing programs with extremely small amounts of memory. That being said, kudos to those who want to write code this way, because I never want to write this kind of code, I find it extremely annoying to read and understand.
@CodeVault
@CodeVault 11 күн бұрын
I think it's a matter of getting used to it. Once you see it enough times and understand it, it doesn't look much different than code in other, higher level languages that is less efficient
@SuperCoolHandle94
@SuperCoolHandle94 2 жыл бұрын
Fantastic video, thank you
@benjaminoyaro1309
@benjaminoyaro1309 3 жыл бұрын
thank you
The sizeof operator in C
6:08
CodeVault
Рет қаралды 29 М.
Bitwise operators in C
13:33
CodeVault
Рет қаралды 33 М.
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
1% vs 100% #beatbox #tiktok
01:10
BeatboxJCOP
Рет қаралды 67 МЛН
Bitwise Operators and WHY we use them
8:41
Alex Hyett
Рет қаралды 102 М.
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 23 М.
Algorithms: Bit Manipulation
9:06
HackerRank
Рет қаралды 544 М.
Bitwise Operators | C Programming Tutorial
12:19
Portfolio Courses
Рет қаралды 27 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Intro to Binary and Bitwise Operators in C++
21:40
The Cherno
Рет қаралды 141 М.
Bit Fields in C. What are they, and how do I use them?
13:26
Jacob Sorber
Рет қаралды 87 М.
Compilers, How They Work, And Writing Them From Scratch
23:53
Adam McDaniel (kiwi)
Рет қаралды 248 М.
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН