Это видео недоступно.
Сожалеем об этом.
Bitwise Operators | C Programming Tutorial
HTML-код
- Опубликовано: 4 сен 2021
- 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...
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!
Thank you Deja, I’m glad you enjoyed the video! :-)
The best ever video. Simple and straightforward
Thank you, I'm glad you enjoyed it! :-D
Love your videos so much you make hard things much easier in life!
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! :-)
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!
The best explanation out there for Bitwise Operators.
that 's beautifully and elegantly explained. Thank you
You're welcome Naboulsi, I'm glad you enjoyed it! :-)
That was a really simple explanation. Thank you so much.
You’re welcome, I’m glad you found it simple! :-)
Nice introduction. Could you make a video showing real world uses and useful tricks that you can do with bit manipulation?
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: ruclips.net/video/-3Rt2_9d7Jg/видео.html.
Very informative and easy to understand
I'm glad you liked it Dennis! :-D
Very good explained! 😊
I’m glad you liked it! :-)
This was very helpful thank you so much !
You're very welcome Abdelhak! :-D
great explanation!
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.
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");
}
}
}
Thank you!
You're welcome! :-)
very clear explanation, thans a lot
You're welcome Moss, I'm glad you enjoyed it! :-)
Is there a sequence in bitwise operation?
for e.g ((i*3) | (i*3+2)
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
Is there a difference between doing math with bitwise operator and arithmetic operator?
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.
thank you so much
You’re welcome! :-D
Shouldn't the compiler complain that "r" get -10 since it's a unsigned int?
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. 🙂
This seems like it could be useful if you don't want to call the library.
Yes we can definitely do some math operations with bitwise operators. :-)