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

Sdílet
Vložit
  • čas přidán 13. 09. 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...]

Komentáře • 29

  • @benjaminshinar9509
    @benjaminshinar9509 Před 4 lety +12

    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 Před rokem +5

    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.

  • @AbrarShaikh2741
    @AbrarShaikh2741 Před 2 měsíci

    Simple clear and concise

  • @edwinontiveros8701
    @edwinontiveros8701 Před 4 lety +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!

  • @marusdod3685
    @marusdod3685 Před 4 lety +38

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

    • @Deorinho
      @Deorinho Před 5 měsíci

      Looool subtle racism will never die

  • @GaussianBluff
    @GaussianBluff Před 4 lety +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.

  • @turkym7md5
    @turkym7md5 Před rokem +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% !!

  • @yekaalohabtemichael5068
    @yekaalohabtemichael5068 Před 4 lety +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.

  • @kraftwerk974
    @kraftwerk974 Před 4 lety

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

  • @rameshb5987
    @rameshb5987 Před 3 lety +2

    Literally u r proving the power of c...

  • @rustycherkas8229
    @rustycherkas8229 Před 2 lety +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.

  • @brianhoskins1979
    @brianhoskins1979 Před rokem

    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.

  • @antoniomellobabo1229
    @antoniomellobabo1229 Před 3 lety

    tHANKS GOD HELPED A LOT

  • @rameshb5987
    @rameshb5987 Před 3 lety

    Am really become ur fan...sir

  • @barulli87
    @barulli87 Před rokem

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

  • @shivam4428
    @shivam4428 Před 4 lety

    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  Před 4 lety +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*: czcams.com/video/IGW8-yeYRgI/video.html
      *with threads*: czcams.com/video/aypNWcRlwYw/video.html

    • @shivam4428
      @shivam4428 Před 4 lety

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

  • @edward3105
    @edward3105 Před 3 lety

    What is the purpose of doing this?

    • @chirathnissanka
      @chirathnissanka Před rokem

      Constant time operations, speed optimized code etc

  • @askr7512
    @askr7512 Před 4 lety +1

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

    • @JacobSorber
      @JacobSorber  Před 4 lety +4

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

    • @askr7512
      @askr7512 Před 4 lety

      @@JacobSorber ☺

  • @hir0085
    @hir0085 Před měsícem +2

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