Это видео недоступно.
Сожалеем об этом.

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...

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

  • @dejakju
    @dejakju Год назад +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

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

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

    The best ever video. Simple and straightforward

  • @dariabuyanovsky8033
    @dariabuyanovsky8033 Год назад +5

    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 11 месяцев назад +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 8 месяцев назад

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

  • @yousraadel5456
    @yousraadel5456 9 месяцев назад

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

    • @PortfolioCourses
      @PortfolioCourses  9 месяцев назад

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

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

    Nice introduction. Could you make a video showing real world uses and useful tricks that you can do with bit manipulation?

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

      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.

  • @zaph254
    @zaph254 2 года назад +2

    Very informative and easy to understand

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

    Very good explained! 😊

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

    This was very helpful thank you so much !

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

    great explanation!

  • @vicsteiner
    @vicsteiner Месяц назад

    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.

  • @youtubeuser2352
    @youtubeuser2352 10 месяцев назад +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");
    }
    }
    }

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

    Thank you!

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

    very clear explanation, thans a lot

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

    Is there a sequence in bitwise operation?
    for e.g ((i*3) | (i*3+2)

    • @PortfolioCourses
      @PortfolioCourses  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

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

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

    • @PortfolioCourses
      @PortfolioCourses  Год назад +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.

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

    thank you so much

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

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

    • @PortfolioCourses
      @PortfolioCourses  Год назад +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. 🙂

  • @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. :-)