Bitwise Operators | C Programming Tutorial

  Рет қаралды 24,202

Portfolio Courses

Portfolio Courses

Күн бұрын

A tutorial on the bitwise operators in C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!
How unsigned numbers are represented: www.tutorialsp...
How negative (signed) numbers are represented with C with Two's Complement: en.wikipedia.o...

Пікірлер: 39
@dejakju
@dejakju Жыл бұрын
The best explanation (for me at least) i've ever heared on how the 'XOR' operator ('^') works was: »think of it as follows: if both operands were different, the bit is set, otherwise not«. as simle as that. and the key(word) to take away here is "different" ;) Very nice video by the way. Excellent job done, sir!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you Deja, I’m glad you enjoyed the video! :-)
@thebusinesschat9036
@thebusinesschat9036 2 жыл бұрын
The best ever video. Simple and straightforward
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you, I'm glad you enjoyed it! :-D
@dariabuyanovsky8033
@dariabuyanovsky8033 Жыл бұрын
Love your videos so much you make hard things much easier in life!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I’m so glad to hear you love the videos Daria, and thank you for the positive feedback that is very encouraging for me to hear that! :-)
@youtubeuser2352
@youtubeuser2352 Жыл бұрын
Kevin, you are saving me one video at a time. Btw I took your linked list course, really helpful. I hope you'll have a DSA course!
@naboulsikhalid7763
@naboulsikhalid7763 Жыл бұрын
that 's beautifully and elegantly explained. Thank you
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Naboulsi, I'm glad you enjoyed it! :-)
@Anonymous-XY
@Anonymous-XY 9 ай бұрын
The best explanation out there for Bitwise Operators.
@yousraadel5456
@yousraadel5456 10 ай бұрын
That was a really simple explanation. Thank you so much.
@PortfolioCourses
@PortfolioCourses 10 ай бұрын
You’re welcome, I’m glad you found it simple! :-)
@Everythingzof
@Everythingzof 3 жыл бұрын
Nice introduction. Could you make a video showing real world uses and useful tricks that you can do with bit manipulation?
@PortfolioCourses
@PortfolioCourses 3 жыл бұрын
Hmmm, maybe I will, thank you for the idea! :-) They come up in places like low-level programming of operating systems, for example: en.wikipedia.org/wiki/Page_table. This actually gets a reference in the movie The Social Network: kzbin.info/www/bejne/Y2S1pWWVbslqgMk.
@zaph254
@zaph254 2 жыл бұрын
Very informative and easy to understand
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I'm glad you liked it Dennis! :-D
@cindaellas
@cindaellas 4 ай бұрын
Very good explained! 😊
@PortfolioCourses
@PortfolioCourses 4 ай бұрын
I’m glad you liked it! :-)
@vicsteiner
@vicsteiner 2 ай бұрын
What bugs me a bit is that if r is unsigned int how do we get a signed -10 when we print the flipped version for the 9? Now I see, in the printf we can use %u instead of %d to print an unsigned int. Also it is funny that I could not find a simple way to print the binary representation. I use %b in the printf, it more or less work but always with a compilation warning.
@abdelhakmezenner2855
@abdelhakmezenner2855 Жыл бұрын
This was very helpful thank you so much !
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're very welcome Abdelhak! :-D
@theintelmedia9355
@theintelmedia9355 3 жыл бұрын
Is there a sequence in bitwise operation? for e.g ((i*3) | (i*3+2)
@PortfolioCourses
@PortfolioCourses 3 жыл бұрын
Yes, the order of operations is part of the order of operations for all operations in C: www.tutorialspoint.com/cprogramming/c_operators_precedence.htm
@youtubeuser2352
@youtubeuser2352 11 ай бұрын
Can someone explain what is happening inside the for loop that makes it able to display the bit pattern of 149. // Given set: SET A = 149 or {7, 4, 0, 2} #include // User-defined data type typedef unsigned char SET; // unsigned is from 0 to 255 // Function declaration void displayBitPattern(SET X); int main() { SET A = 149; // Function call displayBitPattern(A); return 0; } // Function definition void displayBitPattern(SET X) { int bits = sizeof(SET) * 8; int n; for (n = bits - 1; n >= 0; n--) { if (X >> n & 1) { printf("1"); } else { printf("0"); } } }
@abdoo-oo
@abdoo-oo Жыл бұрын
great explanation!
@nyrx7koe327
@nyrx7koe327 Жыл бұрын
Thank you!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@mosslnnx
@mosslnnx Жыл бұрын
very clear explanation, thans a lot
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Moss, I'm glad you enjoyed it! :-)
@shafayet0198
@shafayet0198 Жыл бұрын
Is there a difference between doing math with bitwise operator and arithmetic operator?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
This is a great question Akash, though to be honest I didn't really have a great answer to this question myself. :-) I started researching it myself for fun, and it seems like at least in terms of performance, modern compilers will "do what makes the most sense". So even if we use an arithmetic operator, it could actually compile to bitwise operations if it is optimal for performance. These answers here are interesting: stackoverflow.com/questions/20393373/performance-wise-how-fast-are-bitwise-operators-vs-normal-modulus. I found these answers interesting too: stackoverflow.com/questions/3692992/are-there-any-good-reasons-to-use-bit-shifting-except-for-quick-math.
@ShadowlightSE
@ShadowlightSE 2 жыл бұрын
Shouldn't the compiler complain that "r" get -10 since it's a unsigned int?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
That's a great question! 🙂 We're not really storing a negative integer into r with something like: r = -10; what we're really doing is bitwise operations and storing the result into r, which the compiler can't really be certain of at compile-time (which is when the compiler can flag something as incorrect). We could be initializing x and y using user input, for example, and there would be no way for the compiler to know the result. The other thing is that technically r is just storing "bits of data". It's really just information in memory. When we use %d as a placeholder in printf() what we're telling printf is to "output this information as if it is a signed integer". And so we get -10, because that information represents -10 when it is interpreted as a signed integer. But it's really printf() that's doing that interpretation with the %d operator... at the end of the day it's just "bits" stored in memory that could technically represent different things than signed integers. 🙂
@mongraal2272
@mongraal2272 2 жыл бұрын
thank you so much
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-D
@opshorts3205
@opshorts3205 18 күн бұрын
thnksss
@codingoftheuniverse8355
@codingoftheuniverse8355 2 жыл бұрын
This seems like it could be useful if you don't want to call the library.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Yes we can definitely do some math operations with bitwise operators. :-)
remove() function for deleting files | C Programming Tutorial
4:24
Portfolio Courses
Рет қаралды 7 М.
struct Basics | C Programming Tutorial
24:44
Portfolio Courses
Рет қаралды 138 М.
Остановили аттракцион из-за дочки!
00:42
Victoria Portfolio
Рет қаралды 2,7 МЛН
Intro to Binary and Bitwise Operators in C++
21:40
The Cherno
Рет қаралды 133 М.
Bitwise Operators in C
20:08
Kris Jordan
Рет қаралды 11 М.
Bitwise Operators and WHY we use them
8:41
Alex Hyett
Рет қаралды 77 М.
Bit Fields in C. What are they, and how do I use them?
13:26
Jacob Sorber
Рет қаралды 82 М.
C bitwise operators 🔣
6:47
Bro Code
Рет қаралды 76 М.
How to print in binary
18:35
CodeVault
Рет қаралды 16 М.
Bitwise Operations & Bit Masking
13:08
Learn Learn Scratch Tutorials
Рет қаралды 34 М.
C_18 Operators in C - Part 6 | Bitwise Operators |  C Programming Tutorials
15:21
Jenny's Lectures CS IT
Рет қаралды 418 М.
Остановили аттракцион из-за дочки!
00:42
Victoria Portfolio
Рет қаралды 2,7 МЛН