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

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

Jacob Sorber

4 года назад

53,279 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@RigelOrionBeta
@RigelOrionBeta - 28.05.2023 15:27

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.

Ответить
@brianhoskins1979
@brianhoskins1979 - 18.04.2023 18:21

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.

Ответить
@barulli87
@barulli87 - 07.04.2023 12:42

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

Ответить
@turkym7md5
@turkym7md5 - 05.03.2023 00:13

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% !!

Ответить
@lorensims4846
@lorensims4846 - 26.07.2021 03:54

I used bit masks a lot when programming my Atari 800 but haven't found much use for it since. I recently learned about the bitwise facilities in C++, disappointed that there doesn't seem to tie the bit patterns to real addresses. Looks to be all virtual.
I think bit-twiddling is fun but there doesn't seem to be as much call for as in the old days.

Ответить
@rameshb5987
@rameshb5987 - 07.03.2021 10:52

Literally u r proving the power of c...

Ответить
@rameshb5987
@rameshb5987 - 07.03.2021 10:50

Am really become ur fan...sir

Ответить
@edward3105
@edward3105 - 20.01.2021 14:52

What is the purpose of doing this?

Ответить
@antoniomellobabo1229
@antoniomellobabo1229 - 31.12.2020 04:29

tHANKS GOD HELPED A LOT

Ответить
@kraftwerk974
@kraftwerk974 - 28.08.2020 09:53

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

Ответить
@shivam4428
@shivam4428 - 16.07.2020 20:42

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

Ответить
@marusdod3685
@marusdod3685 - 26.05.2020 00:42

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

Ответить
@yekaalohabtemichael5068
@yekaalohabtemichael5068 - 28.03.2020 12:32

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.

Ответить
@benjaminshinar9509
@benjaminshinar9509 - 24.03.2020 20:19

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.

Ответить
@askr7512
@askr7512 - 24.03.2020 17:27

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

Ответить
@GaussianBluff
@GaussianBluff - 24.03.2020 17:26

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.

Ответить
@edwinontiveros8701
@edwinontiveros8701 - 24.03.2020 17:13

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!

Ответить