What are Bit Masks, and how do I use them? (examples in C)

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ต.ค. 2024
  • Patreon ➤ / jacobsorber
    Courses ➤ jacobsorber.th...
    Website ➤ www.jacobsorbe...
    ---
    What are Bit Masks, and how do I use them? (examples in C) // This is a quick video about Bit Masks and Bit Masking, a topic that a lot of students don't get until long after it would have been useful. This video will show you how to remove unwanted bits, select the few bits you want, or combine bits in useful ways.
    Related videos:
    Bit Fields: • Bit Fields in C. What ...
    Blinking Lights: • Blink! The Hello World...
    More Blinking: • How to Blink an LED in...
    Blinking Lights With Timers/Interrupts: • How to Blink LEDs with...
    ***
    Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
    About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
    More about me and what I do:
    www.jacobsorbe...
    people.cs.clem...
    persist.cs.clem...
    To Support the Channel:
    + like, subscribe, spread the word
    + contribute via Patreon --- [ / jacobsorber ]
    + rep the channel with nerdy merch --- [teespring.com/...]
    Source code is also available to Patreon supporters. --- [jsorber-youtub...]

ความคิดเห็น • 31

  • @benjaminshinar9509
    @benjaminshinar9509 4 ปีที่แล้ว +15

    when i was practicing for job interviews, we had a saying "if you're stuck and don't know the answer, it's probably xor".
    this advice got us through a lot of riddles, and I think it's the reason i got into my current job.
    interviewers love xor.

  • @RigelOrionBeta
    @RigelOrionBeta ปีที่แล้ว +6

    This js also really useful for using less space for many bools.
    Im a games programmer, and whenever i want to save a struct representing a state to a file, or send it over a network, i create an "optimized" version of that struct for the state, and send that over the wire. The optimized version can turn each 8 bools into just a single byte with bit masking.
    This is a technique called bit packing, but it uses bit masking to write and read the bools back and forth from the optimized struct.

  • @marusdod3685
    @marusdod3685 4 ปีที่แล้ว +41

    very informative, hard to find non-indian videos about C these days

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

      Looool subtle racism will never die

    • @jadrsc
      @jadrsc 8 วันที่ผ่านมา +1

      lol

    • @marusdod3685
      @marusdod3685 7 วันที่ผ่านมา

      @@jadrsc butthurt streetshitter detected

  • @hir0085
    @hir0085 2 หลายเดือนก่อน +2

    why is it so hard to understand this. I feel like the biggest idiot atm.

  • @edwinontiveros8701
    @edwinontiveros8701 4 ปีที่แล้ว +10

    Awesome! Bit twiddling is so underrated these days, now that we have memory to spare and having 64+ boolean vars is common grounds. Although, readability and safefy should always be priority, it is good practice to keep bit manipulation fresh on your toolset. Great video as always!

  • @GaussianBluff
    @GaussianBluff 4 ปีที่แล้ว +9

    Literally implemented hardware bitmasks for my (FPGA) Gameboy ALU today!
    They're also really useful for compiler optimizations, say for conditional assignment. Before conditional moves were around, you could run you condition check, setting a register to 1 if the condition is FALSE and to 0 otherwise, then decrement the register, and AND it to your assignment value. Given that 0 decremented is just a chain of 1s, it would act as an "enable" bitmask - letting all bits through - while 1 decremented (i.e. zero) would act as the "disable" bitmask - blocking all bits.
    Given that branches tend to be expensive, I thought this was a really pretty way of avoiding them when I first found the pattern.

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

      Wow, that is a cool idea

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

    I cant thank this guy enough. They are short and so in point. What would it look like if he was my professor teaching me?? I wish i could take it with him? I still dont understand bit minuplation well i don't if that makes me a bad programmer down the road in my computer science field.

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

    Thanks! this is very useful ❤❤ I used it in my c++ platformer game where if a tile has one way collision and you can't jump on it I can check for this by simply using the '&' operator, I can use bitmasks rather than making millions of variables that tell what a tile is capable of, this has compressed my tile maps size by 70% !!

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

    00:24 That's a lot of zeros in that 'hex' value...
    Glad that you corrected the typo (s/b "0b00000001) in later slides...
    It should be noted that 1 is 1 in all bases... "Hex-ish" window dressing does aid, though, by suggesting bit-wise operations.

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

    Literally u r proving the power of c...

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

    can you expand on how a mask can replace if conditions? my mind is blown

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

    Nice roundup. I guess one thing I'd expect you to have talked about is the requirement to shift in some bit mask scenarios. Example: you have a uint16_t and you want to get hold of the literal _value_ of the MSB, but not retain its binary weighting. If that makes sense.

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

    Simple clear and concise

  • @shivam4428
    @shivam4428 4 ปีที่แล้ว

    Sir , tell us about debugging a project which is multi threaded and uses all IPC techniques and uses all process replacement mechanism..... In short a project which is using fully IPC techniques. How to debug that kind of projects .... specially the fork ,exec , ...ie debugging parent and child and thread same time

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

      Wow. That would be quite a project. There are a lot of different IPC techniques. I do have some videos on debugging with threads, fork, and exec. Maybe they will help a bit, and I'll see what I can do in the future.
      *with fork and exec*: th-cam.com/video/IGW8-yeYRgI/w-d-xo.html
      *with threads*: th-cam.com/video/aypNWcRlwYw/w-d-xo.html

    • @shivam4428
      @shivam4428 4 ปีที่แล้ว

      @@JacobSorber yes . It was a project of mine where is had used all techinques .now i have to debug it using gdb . That's why I need a little bit of help

  • @rameshb5987
    @rameshb5987 3 ปีที่แล้ว

    Am really become ur fan...sir

  • @kraftwerk974
    @kraftwerk974 4 ปีที่แล้ว

    In assembly/ML you do that all the time to set or clear certain bits in certain registers. Very clear tutorial. 👍

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

      Thanks. Glad you liked it

  • @antoniomellobabo1229
    @antoniomellobabo1229 3 ปีที่แล้ว

    tHANKS GOD HELPED A LOT

  • @edward3105
    @edward3105 3 ปีที่แล้ว

    What is the purpose of doing this?

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

      Constant time operations, speed optimized code etc

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

    Is there any codes which cure corona in Tamil nadu, India.. ??

    • @JacobSorber
      @JacobSorber  4 ปีที่แล้ว +4

      while (corona.is_spreading?) { noop; }
      Not specific for Tamil nadu, but that seems to be the recommended code around the world.

    • @askr7512
      @askr7512 4 ปีที่แล้ว

      @@JacobSorber ☺