Bitwise Operators | C Programming Tutorial

Поделиться
HTML-код
  • Опубликовано: 23 янв 2025

Комментарии • 39

  • @dejakju
    @dejakju 2 года назад +7

    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  2 года назад +2

      Thank you Deja, I’m glad you enjoyed the video! :-)

  • @thebusinesschat9036
    @thebusinesschat9036 2 года назад +6

    The best ever video. Simple and straightforward

  • @youtubeuser2352
    @youtubeuser2352 Год назад +1

    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!

  • @Anonymous-XY
    @Anonymous-XY Год назад

    The best explanation out there for Bitwise Operators.

  • @naboulsikhalid7763
    @naboulsikhalid7763 Год назад +1

    that 's beautifully and elegantly explained. Thank you

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You're welcome Naboulsi, I'm glad you enjoyed it! :-)

  • @dariabuyanovsky8033
    @dariabuyanovsky8033 2 года назад +5

    Love your videos so much you make hard things much easier in life!

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      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! :-)

  • @vicsteiner
    @vicsteiner 6 месяцев назад +1

    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.

  • @yousraadel5456
    @yousraadel5456 Год назад

    That was a really simple explanation. Thank you so much.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You’re welcome, I’m glad you found it simple! :-)

  • @simplified-code
    @simplified-code 2 года назад +2

    Very informative and easy to understand

  • @cindaellas
    @cindaellas 8 месяцев назад

    Very good explained! 😊

  • @Everythingzof
    @Everythingzof 3 года назад +4

    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: ruclips.net/video/-3Rt2_9d7Jg/видео.html.

  • @abdoo-oo
    @abdoo-oo Год назад +1

    great explanation!

  • @theintelmedia9355
    @theintelmedia9355 3 года назад +2

    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

  • @abdelhakmezenner2855
    @abdelhakmezenner2855 2 года назад +1

    This was very helpful thank you so much !

  • @youtubeuser2352
    @youtubeuser2352 Год назад +1

    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");
    }
    }
    }

  • @shafayet0198
    @shafayet0198 2 года назад +1

    Is there a difference between doing math with bitwise operator and arithmetic operator?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      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.

  • @mosslnnx
    @mosslnnx Год назад

    very clear explanation, thans a lot

  • @nyrx7koe327
    @nyrx7koe327 Год назад +1

    Thank you!

  • @ShadowlightSE
    @ShadowlightSE 2 года назад

    Shouldn't the compiler complain that "r" get -10 since it's a unsigned int?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +4

      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 3 года назад

    thank you so much

  • @opshorts3205
    @opshorts3205 4 месяца назад

    thnksss

  • @codingoftheuniverse8355
    @codingoftheuniverse8355 2 года назад

    This seems like it could be useful if you don't want to call the library.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      Yes we can definitely do some math operations with bitwise operators. :-)