Coding Challenge #120: Bit Shifting

preview_player
Показать описание


References:

Videos:

Related Coding Challenges:

Timestamps:
0:00 Hello!
1:07 What is bit-shifting?
4:23 Let's start implementing
6:49 A function to shift bits
7:55 implementing decimal to binary conversion
14:46 We can now bit shift!
15:51 Inverting the colors
16:59 Thank you for watching!

Editing by Mathieu Blanchette
Animations by Jason Heglund
Music from Epidemic Sound

#binarynumbers #math #p5js #javascript
Рекомендации по теме
Комментарии
Автор

4:52
"Function ..."
**writes something not even remotely resembling the word function**

Remls
Автор

At my school, they taught us decimal to binary conversion like this (and it would solve your "constraint" to 8 bits):
I'm going to show you an example what the procedure is:

Let's say we want to convert 90:

90 / 2 = 45 Remainder 0
45 / 2 = 22 Remainder 1
22 / 2 = 11 Remainder 0
11 / 2 = 5 Remainder 1
5 / 2 = 2 Remainder 1
2 / 2 = 1 Remainder 0
1 / 2 = 0 Remainder 1

Now read the remainders from bottom to the top, and you'll get the binary number from left to right:

1011010

And you could then code your converting algorithm so that it divides by two and saves the remainder until the number that is getting divided reaches 0

polskus
Автор

You are SO the perfect nerd. Loving your uploads! Thanks!

thorzweegers
Автор

he is so passionate about what he does..i want to be like him..😊😍

mayursaroj
Автор

Dude Dan the happiest shiftman.. your excitement is so contagious!

wowatomica
Автор

I was going to make a "clever" joke about Shiftman but you made it for me :(

nick
Автор

I was gonna learn this some time in school in the new few months, but I'm glad you explained this before it happen, will make life a lot easier. your explanations are also better than that of most teachers

AfonsodelCB
Автор

"tijmf6 shiftBits() {

}"
Has a nice ring to it.

OrangeC
Автор

I love your energy man, great explanation btw

GuitarreroDaniel
Автор

please do challenge in Line algorithm like bresenhams line, midpoint line etc.
i love your entire video...

joko
Автор

You got so excited at 13:35 :D you're great

MrRushifyIt
Автор

An easier to convert a number from decimal to binary is to see whether the given number can be divided by 2. If it is then the least significant bit (the one that correspond to the power of 0) will be 0, and if it isn't this bit is going to be 1. Then you just have to take you number and divide it by 2 and repeat the process (as dividing by 2 just shifts the bits to the right by 1 place) until you end up with a value of zero. Then you just fill the higher powers with 0

The algorithm (pseudocode) would be :
Create an empty string
Create a copy of your number which I'll call val
For i between 0 (included) and 8 (excluded) {
If val % 2 == 1 then add 1 to the front of the string, otherwise add 0
val = val/2
If val == 0 then fill the string with zeros from the front until its size is 8 and break from the loop.
}
Return the string

sebguex
Автор

Like it when you cover the old school tricks!

DogwafflDan
Автор

Hi dan, a bit of visual design advice.. if you want something to appear "on" or "off, " you must make the background the darkest (or lightest) color. Otherwise, on and off will be perceived as a similar contrast from the background, so for the viewer it's just kind of confusing. So maybe make the background 51, "off" 100, and "on" 255

alexfrasca
Автор

Ive added both kinda shifts by number of bits user specifies.
:D, man you really have developed my interest in coding again...

shervilgupta
Автор

Mr SHIFTman you did some good bit of bit SHIFTing on this SHIFT.

WildAnimalChannel
Автор

Yup it's interesting because binary is 2 number system while decimal is 10 number system. If you want to get hundreds from thousands just divide it by 10. Same concept applies in any number system, in binary divide it by two, in nonal divide it by 9, in hexadecimal divide it by 16, and so on.

willysatrionugroho
Автор

function DecToBinStr(value)
{
if(!value) return "0";
let output = "";
while(value > 0)
{
output = (value & 1) + output;
value >>= 1;
}
return output;
}

pidi
Автор

Oh Dan, you could look at showing how bit flags work with masking in your next video! Also, if you wanna take this idea to the extreme to really solidify the idea of bit shifting and masking, you could implement a bitmap. Every bit would be like an element in a Boolean array, just stored more compactly as bits.

kamoroso
Автор

10:52 "Is it weird how much fun this is for me?" Dunno, Is it weird how much I enjoy watching you have fun in front of a video camera in a closet?

gregcampbell