Bitwise Operators | C Programming Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ต.ค. 2024
  • 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 ปีที่แล้ว +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! :-)

  • @Anonymous-XY
    @Anonymous-XY 10 หลายเดือนก่อน

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

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

  • @yousraadel5456
    @yousraadel5456 11 หลายเดือนก่อน

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

    • @PortfolioCourses
      @PortfolioCourses  11 หลายเดือนก่อน

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

  • @zaph254
    @zaph254 2 ปีที่แล้ว +2

    Very informative and easy to understand

  • @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: th-cam.com/video/-3Rt2_9d7Jg/w-d-xo.html.

  • @abdelhakmezenner2855
    @abdelhakmezenner2855 2 ปีที่แล้ว +1

    This was very helpful thank you so much !

  • @cindaellas
    @cindaellas 5 หลายเดือนก่อน

    Very good explained! 😊

    • @PortfolioCourses
      @PortfolioCourses  5 หลายเดือนก่อน

      I’m glad you liked it! :-)

  • @abdoo-oo
    @abdoo-oo ปีที่แล้ว +1

    great explanation!

  • @nyrx7koe327
    @nyrx7koe327 ปีที่แล้ว +1

    Thank you!

  • @mosslnnx
    @mosslnnx ปีที่แล้ว

    very clear explanation, thans a lot

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

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

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

  • @vicsteiner
    @vicsteiner 3 หลายเดือนก่อน

    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 ปีที่แล้ว +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 ปีที่แล้ว +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 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. 🙂

  • @opshorts3205
    @opshorts3205 หลายเดือนก่อน

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