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

  Рет қаралды 8,380

Code With Huw

Code With Huw

Күн бұрын

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!

Пікірлер: 26
@j-p-d-e-v
@j-p-d-e-v Ай бұрын
Im glad I found your channel. Thank you very much!
@khetanshusingh9059
@khetanshusingh9059 Ай бұрын
Great explanation. You made it look so easy. Thanks a lot
@LearnWithHuw
@LearnWithHuw Ай бұрын
Many thanks. I'm glad it was useful.
@adityakharbanda4080
@adityakharbanda4080 Жыл бұрын
this was so helpful in solving the "bulbs" problem in CS50. Thanks a lot sir!
@LearnWithHuw
@LearnWithHuw Жыл бұрын
Glad to help ( though I have no idea what the "bulbs" problem is :-) )
@jamesbush7717
@jamesbush7717 Жыл бұрын
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 Жыл бұрын
Thank you that really illustrates the idea
@LearnWithHuw
@LearnWithHuw Жыл бұрын
Many thanks. Glad it was useful.
@daggerone3370
@daggerone3370 Жыл бұрын
Underrated video.
@LearnWithHuw
@LearnWithHuw Жыл бұрын
Thank you!
@dimpe5038
@dimpe5038 Жыл бұрын
THANK YOU
@LearnWithHuw
@LearnWithHuw Жыл бұрын
I'm glad it was helpful.
@wabdyli
@wabdyli Жыл бұрын
Nice!
@LearnWithHuw
@LearnWithHuw Жыл бұрын
Thanks!
@jamesbush7717
@jamesbush7717 Жыл бұрын
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 11 ай бұрын
He wanted the string representation of 0 and 1 printed out though
@pppcnn9291
@pppcnn9291 Жыл бұрын
How to do if it is a signed decimal?
@LearnWithHuw
@LearnWithHuw Жыл бұрын
I have a video about signed/unsigned here: kzbin.info/www/bejne/l3nZeX6sbqeHkMk
@jamesbush7717
@jamesbush7717 Жыл бұрын
There’s no such thing as an unsigned decimal.
@yannisyannaros4779
@yannisyannaros4779 Жыл бұрын
how does one choose the mask
@LearnWithHuw
@LearnWithHuw Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
@@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 4 ай бұрын
"advanced orogramming" ...you're just saying that to make me feel better aren't you.
@LearnWithHuw
@LearnWithHuw 4 ай бұрын
Ha! But just think, once you learn this stuff you'll be able to baffle all those other programmers!😝
The ONE Thing Most C Programmers Get Wrong!
11:02
Code With Huw
Рет қаралды 28 М.
Bitwise Operations & Bit Masking
13:08
Learn Learn Scratch Tutorials
Рет қаралды 33 М.
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 69 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 55 МЛН
Learn Any Programming Language In 3 Hours!
22:37
Code With Huw
Рет қаралды 308 М.
Convert binary to decimal | C Programming Example
9:06
Portfolio Courses
Рет қаралды 12 М.
How do I access a single bit?
11:07
Jacob Sorber
Рет қаралды 20 М.
Add Two Numbers Without The "+" Sign (Bit Shifting Basics)
18:25
Back To Back SWE
Рет қаралды 123 М.
Object Oriented Programming is not what you think it is. This is why.
13:36
How To Convert Decimal to Binary
13:24
The Organic Chemistry Tutor
Рет қаралды 4,4 МЛН
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 294 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 69 МЛН