Bit Masking Made Easy - Convert Decimal to Binary (advanced programming)

Sdílet
Vložit
  • čas přidán 22. 07. 2024
  • Bit masking is an advanced programming technique. In this example, I show you how to use a bitmask in C in order to determine the bit pattern of a specific byte (a number or a character).
    MORE LESSONS
    =============
    To follow all this series on bits, bytes and binary numbers, go to the “Down To The Metal” playlist:
    • Down To The Metal
    Lessons on specific topics:
    Why Do Big Numbers Turn Negative?
    • Why Do Big Numbers Tur...
    What is a Binary Number?
    • What Is A Binary Number?
    Bits, Bytes and Binary Numbers:
    • Bits, Bytes and Binary...
    Be sure to subscribe to the Code With Huw channel so that you get a notification when I upload more lessons in this series.
    PROGRAMMING BOOKS
    ====================
    If you want to learn programming in more depth (and also support this channel!) you might think of buying one of my books. I have written books on C programming, Using Pointers in C, Recursion and other programming topics. You can buy my books (paperback or Kindle) on Amazon.
    “CODE WITH HUW” ON TWITTER:
    =================================
    / codewithhuw
    “CODE WITH HUW” ON FACEBOOK:
    =================================
    / codewithhuw
    Good luck! And good programming!

Komentáře • 26

  • @j-p-d-e-v
    @j-p-d-e-v Před měsícem

    Im glad I found your channel. Thank you very much!

  • @adityakharbanda4080
    @adityakharbanda4080 Před rokem +2

    this was so helpful in solving the "bulbs" problem in CS50. Thanks a lot sir!

    • @LearnWithHuw
      @LearnWithHuw  Před rokem

      Glad to help ( though I have no idea what the "bulbs" problem is :-) )

  • @khetanshusingh9059
    @khetanshusingh9059 Před měsícem

    Great explanation. You made it look so easy. Thanks a lot

    • @LearnWithHuw
      @LearnWithHuw  Před měsícem +1

      Many thanks. I'm glad it was useful.

  • @jamesbush7717
    @jamesbush7717 Před rokem +1

    The ternary conditional operation returns values - it doesn’t “do” anything [reference 5:30]; however, if you need to perform work based on the result of a given condition, you can encapsulate your code in a statement expression (or is it an expression statement?)or you can encapsulate your code in a block (Blocks.h).

  • @enaskhaled3707
    @enaskhaled3707 Před rokem

    Thank you that really illustrates the idea

  • @daggerone3370
    @daggerone3370 Před rokem

    Underrated video.

  • @dimpe5038
    @dimpe5038 Před rokem

    THANK YOU

  • @wabdyli
    @wabdyli Před rokem

    Nice!

  • @jamesbush7717
    @jamesbush7717 Před rokem +1

    You could have just written “c & mask” without the ternary conditional. That it is shorthand for the traditional if-else clause is not relevant; that it can be abbreviated to just an else clause would be interesting to note, though.

    • @ironfist7789
      @ironfist7789 Před 11 měsíci

      He wanted the string representation of 0 and 1 printed out though

  • @pppcnn9291
    @pppcnn9291 Před rokem

    How to do if it is a signed decimal?

    • @LearnWithHuw
      @LearnWithHuw  Před rokem

      I have a video about signed/unsigned here: czcams.com/video/aHvHKv9BTZg/video.html

    • @jamesbush7717
      @jamesbush7717 Před rokem

      There’s no such thing as an unsigned decimal.

  • @yannisyannaros4779
    @yannisyannaros4779 Před rokem

    how does one choose the mask

    • @LearnWithHuw
      @LearnWithHuw  Před rokem

      The mask is exactly as shown in this video - with a single bit set to 1 and all others to 0. Then the single 1 is shifted at each turn through the loop.

    • @championclarke2872
      @championclarke2872 Před rokem

      Hi , what I was actually asking is how does one choose a mask in general ... not for this particular application - for another application. - I mean say for example , for this particular application what was your thinking behind choosing this mask (1000000). sorry if i Am i asking a stupid question ?

    • @LearnWithHuw
      @LearnWithHuw  Před rokem +2

      @@championclarke2872 I need to test 1 bit at a time. That's why the mask is 10000000. The "on" (1) bit is then shifted to 01000000 and then to 00100000 and so on. So the important idea to get is that only 1 bit in the mask is ever 1 but that the 1 bit is shifted so that eventually every bit in a byte can be tested (if 1 in the mask and 1 in the "tested byte" are both 1 then the result is also 1).

    • @NikolaNevenov86
      @NikolaNevenov86 Před rokem +3

      @@championclarke2872 You choose the mask based on what you are looking for. In this case Huw is trying to display the bits. So he needs a bitmask where he has only one bit "on" or as "1" at the start or end of the bit. Most code I've seen use an actual int "1" as a bitmask since it's bits are 00000001. But then they bit shift that to the left instead to the right as Huw did. So the same concept but starting from the different side. The goal here is to go through all the bits of the variable and check if they are "on" or 'off", so you really need a mask that has only 1 bit turned on, at either end(depending from where you are starting), that you can move using the bitshift operation.
      For other applications, the sky is the limit. Since the way bitmasks and bitfields are used(from what I've seen) is using the byte as a (memory)cheap "array" of boolean values, for enabling features. For example in the Vulakn Graphics API, they use the bitfields for features you can enable or features the GPU supports. So if you want to know which features your device supports you have to "AND" the returned value from Vulkan with a bitmask. So let's say we ask Vulkan about our device, and it returns a value who's bits are 00001111. And you are looking for the device's Graphics(that it can draw) and Prent(it can show what it drew on screen) capabilities. Let's say Graphics is 00000100 and Present is 00000001. Thus your mask should be 00000101. So you should bitwise "AND" the return value from Vulkan 00001111 with 00000101 which will result in non "0"/true(actually it will return a result that is the mask) thus your check says "Yes your device supports both or just one of Graphics and Present". And if you want to cover all cases you can check if the actual int value you get from the "AND" is logically equal "==" to your mask(comparing the two ints), if it's not this would mean that only one (Graphics or Present) is available.
      If you want to check for more features you just create that mask for those. And honestly most people don't remember these masks so in Vulakn(for example) they have enums with all the bit values they've used. So when making the mask you just use the bitwise OR operator.

  • @ratslaydownflat2540
    @ratslaydownflat2540 Před 4 měsíci

    "advanced orogramming" ...you're just saying that to make me feel better aren't you.

    • @LearnWithHuw
      @LearnWithHuw  Před 4 měsíci

      Ha! But just think, once you learn this stuff you'll be able to baffle all those other programmers!😝