Bitwise Operators and WHY we use them

  Рет қаралды 58,916

Alex Hyett

Alex Hyett

Күн бұрын

Bitwise operators are one of the least understood parts of programming when you are first starting out. In this video, I cover what they are and why we use them!
☕️ I don’t accept paid sponsorships and I have turned off mid-video ads. You’re welcome! If you would like to support my channel see: ko-fi.com/alexhyett
📨 Join my free weekly newsletter for advice, technology and more: newsletter.alexhyett.com
🗺️ Backend Developer Roadmap: www.alexhyett.com/backend-dev...
📝 Related blog post: www.alexhyett.com/bitwise-ope...
🎓 RECOMMENDED COURSES (Use code FRIENDS10 for 10% off)
The Complete Web Developer Course - academy.zerotomastery.io/a/af...
The Complete Junior to Senior Web Developer Roadmap - academy.zerotomastery.io/a/af...
Complete SQL + Databases Bootcamp - academy.zerotomastery.io/a/af...
📚 RECOMMENDED BOOKS
Software Development
Clean Code - geni.us/5AEwj2
Clean Architecture - geni.us/yBrTX
Domain Driven Design - geni.us/WiR0Q0x
Design Patterns - geni.us/5ncUt
Developer Hegemony - geni.us/lAXy
Pragmatic Programmer - geni.us/GfNj9
Data Structures and Algorithms Made Easy - geni.us/sqg6kJ
Refactoring - geni.us/ufAP0mE
The Productive Programmer - geni.us/IT9WiN
Pragmatic Thinking and Learning - geni.us/x81A
Test Driven Development - geni.us/HFV52
Microservices - geni.us/8vMA
🚀 MY FAVOURITE TOOLS
Visual Studio Code - code.visualstudio.com/
Obsidian - obsidian.md/
Notion - affiliate.notion.so/alexhyett
⏳ TIMESTAMPS
00:00 Introduction
00:43 Why do we use Bitwise Operators?
01:55 Unix file permissions
02:54 6 Bitwise Operators
03:01 AND Operator
04:08 OR Operator
05:17 XOR Operator
06:01 NOT Operator
06:30 Negative Numbers
07:24 Left Shift and Right Shift
You can check out more of my favourite tools on my website:
www.alexhyett.com/tech/
🔗 MY KEY LINKS
🌍 Blog - www.alexhyett.com/
🐘 Mastodon - social.alexhyett.com/@alex
🧑‍💻WHO AM I
I’m Alex, a Software Developer and KZbin working in the UK. I make videos about software development to help developers with the skills they need to be senior developers. As well as this KZbin Channel, I also write articles on my website (alexhyett.com) as well as write a regular newsletter that contains some thoughts to help aspiring developers.
‼️ DISCLAIMERS
Some of the links in this description are affiliate links, which I get a small commission, at no extra cost to you 🙂. I appreciate you supporting my channel so I can continue providing you with free software development content!
#coding #programming #developer

Пікірлер: 109
@loayalsaid6343
@loayalsaid6343 10 ай бұрын
small missed detail, : the bitwise, & and | or whatever, they look if both are set, meaning both are one, not just matching, because if both are 0 for example, & will give zero and not 1 The video is amazing, Thank you, really!.
@alexhyettdev
@alexhyettdev 10 ай бұрын
That is a very good point, I should have mentioned that in the video. Thank you for pointing it out. For those finding this comment in the future: IF: a = 1 b = 1 THEN: a & b = 1 IF: a = 0 b = 0 THEN: a & b = 0
@doyoufeel...thatyoulackcri6760
@doyoufeel...thatyoulackcri6760 3 ай бұрын
I mean... how can anyone call himself a programmer and then not know the most basic of how computers operate, which literally can be learned in one day? This is like claiming to be a matematician, but not knowing how addition works. I will never ever trust any programmer who doesn't know the most basic of his own tool.
@loayalsaid6343
@loayalsaid6343 3 ай бұрын
I know, and appreciate what you said, but you know, mistakes happens, and he said he forgot about it, now he does not know about it. buy anyway, Good luck with your learniing process.
@witchoutcandy
@witchoutcandy 3 ай бұрын
so in the XOR example: READ_WRITE = 4 WRITE_PERMISSION = 2 EXECUTE_PERMISSION = 1 userPermissions ^= WRITE_PERMISSION this equals 4 because 6^2 is 110^010, that IS 100, right? i've thinking... the userPermissions was 7 because of the last operator example, so 111^010 was going to be 5, and the userPermissions "lost" WRITE_PERMISSION but didnt lost execute_permission and read_permission :0 epic
@gabeznl3591
@gabeznl3591 Ай бұрын
Also, how can the binary number 0011 equals to 6? It's always 3 in decimal...
@johningram2153
@johningram2153 3 ай бұрын
I've been using the term "bit" when explaining unix permissions ot people for over 25 years, and never understood why. Great video.
@lanniekins666
@lanniekins666 Жыл бұрын
Thank you for this! I've been a game developer for a while and had never had to deal with this concept, but it's been thrown at me with very little explanation in the cybersec course I'm taking. This was very helpful :)
@alexhyettdev
@alexhyettdev Жыл бұрын
I am glad you found it helpful. They are definitely one of the lesser used bits of programming.
@timi_t_codes
@timi_t_codes 11 ай бұрын
You keep things so simple!! I would love to teach like you do.
@alexhyettdev
@alexhyettdev 10 ай бұрын
Thank you for the kind words! I have never really taught anything before doing this channel. You never know what you can do!
@numbertumbers529
@numbertumbers529 Жыл бұрын
This was very well-made and informative! You deserve a lot more than 94 subscribers!
@alexhyettdev
@alexhyettdev Жыл бұрын
Thank you very much, I am glad you liked it.
@bouzie8000
@bouzie8000 2 ай бұрын
oh how much he has grown since this comment
@luis.barragan
@luis.barragan Жыл бұрын
This was a really professional video, straight to the point. Thanks for saving my day.
@alexhyettdev
@alexhyettdev Жыл бұрын
You are welcome. I am glad you found it useful.
@taylorvicente7325
@taylorvicente7325 Ай бұрын
You explained in 8 minutes what my CS professor couldn’t explain in an hour! Great video!
@ShermukhammadKarimov
@ShermukhammadKarimov 4 ай бұрын
Thank you so much. You helped me with my university exams. Keep going. Well done.
@senriofthealexis3998
@senriofthealexis3998 2 ай бұрын
Very concise and informative, thank you Alex
@PauloGosen
@PauloGosen 4 ай бұрын
Thank you Alex, I really learned something with this video. Keep it up
@paval1s69
@paval1s69 Жыл бұрын
Nice! Already learned it but it was still great as a refresher and also a great introduction for newbies to the topic. A shame that the channel is not that big yet
@alexhyettdev
@alexhyettdev Жыл бұрын
Thanks! I have only started posting regularly at the start of this month so still early days 🤞🏻
@arhanghosh2744
@arhanghosh2744 2 ай бұрын
Wonderfully explained. Great video
@Ctrl_Alt_Elite
@Ctrl_Alt_Elite 7 ай бұрын
I'm learning DSA atm, and came across a constant time solution for the Power of 2 problem that uses the AND bitwise operator AMAZING videos, watched both the binary & bitwise operators vids and learned a tonne! Thanks a lot! 😁
@alexhyettdev
@alexhyettdev 7 ай бұрын
Thanks for sharing, I am glad my videos could help!
@bouzie8000
@bouzie8000 2 ай бұрын
Informative and concise video. Good stuff
@abeldamtew2004
@abeldamtew2004 Ай бұрын
thanks for the awesome explanation and it is good to know when and how to use them.
@SaMcfc05
@SaMcfc05 9 ай бұрын
Thank you, this makes things a lot simpler to understand
@alexhyettdev
@alexhyettdev 9 ай бұрын
You’re welcome!
@DaveDemers69
@DaveDemers69 Жыл бұрын
Thank you, this helped a ton!
@alexhyettdev
@alexhyettdev Жыл бұрын
Glad it helped!
@hernanphillip334
@hernanphillip334 11 ай бұрын
Very very nice video sir, it has helped me out a lot!
@alexhyettdev
@alexhyettdev 11 ай бұрын
That's great! I am glad it helped.
@Tech-Dev
@Tech-Dev 6 ай бұрын
Cheers for making this concept so easy to understand.
@alexhyettdev
@alexhyettdev 5 ай бұрын
You're very welcome!
@amaniabid4476
@amaniabid4476 Жыл бұрын
thank you for this because I get it very easy when you explain it
@alexhyettdev
@alexhyettdev Жыл бұрын
You’re welcome! I am glad I could help.
@mr.luxxyandreneo5168
@mr.luxxyandreneo5168 11 ай бұрын
Sir really , the rwx ex. from linux was amazing , none told us such deeply, thanks for relating bin no with programming & with linux, just amazing
@alexhyettdev
@alexhyettdev 11 ай бұрын
Thank you for the kind words. I am glad my video was helpful!
@AFriendOfYours0
@AFriendOfYours0 Жыл бұрын
Thank you so much. I was trying to make sense of some code I came across on GitHub and I had no idea what was going on until I watched this. Didn't think I'd have to deal with bitwise operators in JavaScript haha. Loved the simple explanation and the lighting/graphics.
@alexhyettdev
@alexhyettdev Жыл бұрын
They are definitely quite confusing if you haven’t seen them before. Even if you have it can take a little while to work out what is actually doing.
@mossabDiae
@mossabDiae 10 ай бұрын
Great video Alex and well explained! I've been using Linux in a while and it's the first time to know the origin behind choosing those exact numbers for files permission, learned a lot! Thank you, subscribed.
@alexhyettdev
@alexhyettdev 10 ай бұрын
Thank you! Yes I don't think many people realise where those numbers come from. It does seem quite arbitrary at first glance.
@ShiloBuff
@ShiloBuff 2 ай бұрын
Great explanation. Thanks
@davidvillalobos3854
@davidvillalobos3854 10 ай бұрын
good video,very simple, straight to the point and the example of user permission was great and easy to follow. Thanks
@alexhyettdev
@alexhyettdev 10 ай бұрын
Thank you, I am glad I could help!
@Honest_Reply900
@Honest_Reply900 Ай бұрын
Very informative video, thanks and making and sharing it
@blaxpow
@blaxpow Жыл бұрын
That's excellent content. Good explanation and video editing. +1 subscriber 🎉
@alexhyettdev
@alexhyettdev Жыл бұрын
Thanks Wesley! Glad to have you here, welcome 👋
@moishie20
@moishie20 2 ай бұрын
Thanks. Great video
@MrKruges
@MrKruges 2 ай бұрын
This is so helpful. Thank you!
@alexhyettdev
@alexhyettdev 2 ай бұрын
You're welcome!
@wangxuerui9949
@wangxuerui9949 2 ай бұрын
What an awesome video! Super helpful!!
@alexhyettdev
@alexhyettdev 2 ай бұрын
Thank you, I am glad you liked it.
@vincentzacks9584
@vincentzacks9584 2 ай бұрын
Excellent explanation, thanks mate!
@alexhyettdev
@alexhyettdev 2 ай бұрын
You're welcome!
@Xander95X
@Xander95X 6 ай бұрын
Wow, this video is great, super succinct and explained very clearly.
@alexhyettdev
@alexhyettdev 6 ай бұрын
Thank you I am glad it helped!
@mikeh7704
@mikeh7704 3 ай бұрын
This reminds me of the days I was learning Z80 machine language programming. Nice video! Are there no bitwise rotate operators, or are they rarely needed in higher-level languages?
@wypimentel
@wypimentel 3 ай бұрын
Yeah, this topic was scary to me but you make it seems easy, thanks!
@Dallascalo
@Dallascalo 6 күн бұрын
Now it makes more sense!
@bijjepurushotham7667
@bijjepurushotham7667 2 ай бұрын
nice explanation
@IndisputableConnoisseur
@IndisputableConnoisseur Жыл бұрын
this is gold
@alexhyettdev
@alexhyettdev Жыл бұрын
Thank you for the kind comment. I am glad it was useful.
@MrOvergaming
@MrOvergaming Ай бұрын
I clicked on the like button before strarting the video!
@angelitocasasis5947
@angelitocasasis5947 18 күн бұрын
damn man, you should've been my professor in college. now I'm gonna your student on every vid. cheers!
@oglothenerd
@oglothenerd 9 ай бұрын
Well, I just learned something cool about UNIX file permissions! I love this, because I use Arch, btw.
@alexhyettdev
@alexhyettdev 9 ай бұрын
That's great! I used Arch a lot when I was younger. Back when I had time to customise everything lol
@oglothenerd
@oglothenerd 9 ай бұрын
@@alexhyettdev Lol.
@oglothenerd
@oglothenerd 9 ай бұрын
@@alexhyettdev I am 14, so, yeah... XD
@alexhyettdev
@alexhyettdev 9 ай бұрын
@@oglothenerd I remember Arch Linux being really fast. I screwed something up though and couldn’t boot. 🤦🏻‍♂️
@dumebiubogu3445
@dumebiubogu3445 20 күн бұрын
Thanks for the video. Not sure I am understanding the point of the 2's compliment. Does that give us the answer of ~5 ? So ~5 is -6?
@whenlifegivesyouLSD
@whenlifegivesyouLSD 8 ай бұрын
This channel is a gem
@alexhyettdev
@alexhyettdev 8 ай бұрын
Thanks, I am glad you like it!
@artemmozharov2492
@artemmozharov2492 3 ай бұрын
you can use shifts to multiply by 2pow n and there is also >>> operator or
@kevinpellerin6715
@kevinpellerin6715 Жыл бұрын
Programming can be so high level, coming from microprocessors, its a little hard to not have to think about registers and interrupts.
@nazarinebeats599
@nazarinebeats599 8 ай бұрын
Im confused, at 4:06 the output is can read, but it looks to me that the result form the bit and should not match so I expected to see 0 ?
@alexhyettdev
@alexhyettdev 8 ай бұрын
I don't think I explained that well in this video. As one of the other comments points out (see pinned) for & operator if both bits are 1 then it will equal 1, if both bits are 0 it will equal zero. If they are both different it will be 0. So our user permissions are set to 6, which is 110 in binary. The read permission is 4 which is 100. If you do 110 & 100 you will get 100, which is why you can do: if (userPermissions & READ_PERMISSION) == READ_PERMISSION:
@MoolsDogTwoOfficial
@MoolsDogTwoOfficial Ай бұрын
This'll be helpful for going into raw mode (UNIX).
@kevinbatdorf
@kevinbatdorf 6 ай бұрын
What are some common use cases for the last three you mentioned? ~ >
@kdt85
@kdt85 7 ай бұрын
what practical uses do the left and right bit shifts have?
@alexhyettdev
@alexhyettdev 7 ай бұрын
They do have some mathematical uses. Doing x > 5 is equivalent of dividing x by 2 to the power of 5. They are also sometimes used for enum definitions which you can see here: stackoverflow.com/questions/3999922/why-use-the-bitwise-shift-operator-for-values-in-a-c-enum-definition If anyone has any other uses for them, I would be interested to know too!
@kdt85
@kdt85 7 ай бұрын
@@alexhyettdev thanks!
@JacksonBockus
@JacksonBockus Ай бұрын
I use them all the time! Some hardware peripherals will store numerical values in weird places within registers, so to extract them and use them as numbers you need to perform a shift so that each bit is in the right place. Say we had an 8 bit register used by a temperature sensor with the lower three bits being for control and the upper five bits actually representing the temperature. You can extract the temperature by reading the value of the register and doing a left shift by three.
@kdt85
@kdt85 Ай бұрын
@@JacksonBockus thank you, yes I've come to realise they are widely used in embedded systems to directly access the pins by manipulating the register. Arduino for example used an extraction layer to turn pins on and off, if you use bit wise it's much faster and parallel
@jumbo999614
@jumbo999614 17 күн бұрын
Please tech me this code Below is part of code that someone wrote for making dot matrix led 8x8 text scrolling byte bitmap[8][7]; int numZones = sizeof(bitmap) / 8; // One Zone refers to one 8 x 8 Matrix ( Group of 8 columns) int maxZoneIndex = numZones - 1; int numCols = numZones * 8; // Converts row and colum to bitmap bit and turn it off/on void Plot(int col, int row, bool isOn) { int zone = col / 8; int colBitIndex = x % 8; byte colBit = 1
@moonasha
@moonasha 10 ай бұрын
in unity we use these for layer masks. For whatever reason, layers are stored as a series of booleans in a 32 bit (i think) integer... so if we want to mask off layer 7, we have to bit shift 1 over 7 places, 1
@alexhyettdev
@alexhyettdev 10 ай бұрын
That's interesting, thanks for sharing. I have been meaning to try out Unity.
@oreokarail
@oreokarail Жыл бұрын
Don't stop. There's a long way to go...
@alexhyettdev
@alexhyettdev Жыл бұрын
Yes it is definitely a long journey!
@oglothenerd
@oglothenerd 9 ай бұрын
I am needing to use them because I am making a voxel game.
@alexhyettdev
@alexhyettdev 9 ай бұрын
That’s awesome. I have had “make a game” on my todo list for a long time. One of these days.
@oglothenerd
@oglothenerd 9 ай бұрын
@@alexhyettdev Highly recommend Rust with the Bevy engine!
@SurajAbdullai
@SurajAbdullai 5 ай бұрын
@@alexhyettdev Hi pls my account got banned and i need your help
@realdragon
@realdragon 5 ай бұрын
I discovered using bitwise operators is faster than dividing by 2 for floats
@alexhyettdev
@alexhyettdev 5 ай бұрын
Good to know! Thanks for sharing.
@leonardeuler4
@leonardeuler4 3 ай бұрын
Which language?
@realdragon
@realdragon 3 ай бұрын
@@leonardeuler4 C
@Debianguy31
@Debianguy31 3 ай бұрын
​@@leonardeuler4any Language
@martinfinch5011
@martinfinch5011 2 ай бұрын
Also multiplying by 2 using the bit shift left operator is faster as well I believe
@seanfull7123
@seanfull7123 8 ай бұрын
Teach my class plz
@alexhyettdev
@alexhyettdev 8 ай бұрын
Is your teacher not very good, or just boring? 🤣
@meerkatpowergaming9412
@meerkatpowergaming9412 10 ай бұрын
This video is a bit loaded for someone who is truly a beginner.
@alexhyettdev
@alexhyettdev 10 ай бұрын
Noted. I will try and do some more videos for true beginners in the future.
@meerkatpowergaming9412
@meerkatpowergaming9412 10 ай бұрын
@@alexhyettdev Yeah I just find it truly hard to find resources for others on this specific subject. Even 10 years ago.
8 DATA STRUCTURES You NEED to Know
10:50
Alex Hyett
Рет қаралды 8 М.
WHY DOES SHE HAVE A REWARD? #youtubecreatorawards
00:41
Levsob
Рет қаралды 31 МЛН
SOLID Principles: Do You Really Understand Them?
7:04
Alex Hyett
Рет қаралды 118 М.
Bit Fields in C. What are they, and how do I use them?
13:26
Jacob Sorber
Рет қаралды 78 М.
What Are ORMs and Why Is Prisma Falling Behind
13:15
Ljupche Vasilev
Рет қаралды 425
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,3 МЛН
Bitwise Operations & Bit Masking
13:08
Learn Learn Scratch Tutorials
Рет қаралды 32 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 271 М.
how NASA writes space-proof code
6:03
Low Level Learning
Рет қаралды 2 МЛН
Bitwise Operations tutorial #1 | XOR, Shift, Subsets
12:04
Errichto Algorithms
Рет қаралды 205 М.