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
or you can type (90 >>> 0).toString(2) that is way more faster than the cyclomatically complex algorithm you are trying to use. It was fun in the 90s when you had to optimize computation for your game to run smoothly on a i386. But nowadays, the native methods are going to be faster, anytime.
@@kirkanos771 Yes, Daniel even mentioned that he could just use num.toString(2), but he wanted to show the conversion from decimal to binary. What I don't understand in your proposed code though is why you want to logically shift right by zero and then convert to binary.
IceCreeper28 You can ignore the triple shift operator, it is just there to prevent shifting negative numbers. That could give undesirable effects. It's up to you to use it if you intend to manage the binary representation of negative numbers (it's band dependant though, 32 bits ? yep, i just tried in the console).
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
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
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.
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
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.
You could have put the updating of the byte array right into the decimalToBinary for loop. Would have saved CPU cycles. This would of course require a rename of the decimalToBinary function. Maybe decimalToByteArray or so.
Decimal to binary is easy: int n = 255 char[] byte = new char[8]; int c = 7; while(n!=1){ char[c] = n%2 == 0 ? 1 : 0; n /= 2; c--; } ( I dont know if this works 100%, but i have a code that uses the same logic ( n%2 ) that works fine )
Is it possible to request a topic be covered? If so, I'd really like help with concave polygon triangulation and collision detection. I haven't found good explanations online.
I wanted to like this video- you seem very knowledgeable and can definitely explain things well.... the problem I found is that the presentation was a bit ‘scatter brain’. The explanation jumps around too much and you reference things we don’t know about.. I’m already confused about *why* we need to binary shift but I only made it to 06:34 before I bailed. This would be good as a first take...
No, i mean when i use any function outside of draw() or setup() p5js gives me - "Did you just try to use p5.js's rect() function? If so, you may want to move it into your sketch's setup() function. " - This Warning Message, but how does p5js knows that i am using it outside of setup() or draw()?
@@anuraghazra4772 you can create your own functions right? Well if you "create" a function that is part of p5 it will first try to treat it as your own function but then see it exists and realise that it should be in setup or draw. That's what I think.
@@anuraghazra4772 my guess would be because technically everything is being called via either the setup or draw, e.g show has rect/ellipse functions but it gets called from draw, maybe you're trying to call these functions in a different way? It'll give you a warning because I believe the draw function is supposed to know what is being drawn and external calls might mess with it
I dont know how they do it, but a possible way to do: function onload() { //.... //otherstuff here SETTINGUP = true; setup(); SETTINGUP = false; //otherstuff here //.. } function size() { if(SETTINGUP ) { //... } else console.debug("Warning message blah blah blah .."); } Anyway thats just my guess. (I didnt check if thats what they do).
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
or you can type (90 >>> 0).toString(2) that is way more faster than the cyclomatically complex algorithm you are trying to use. It was fun in the 90s when you had to optimize computation for your game to run smoothly on a i386. But nowadays, the native methods are going to be faster, anytime.
That's cool. So it'd be something like this psuedo code:
x = 90, n[]
while (x)
n.insert(0, x % 2)
x = x / 2
end
@@kirkanos771 Yes, Daniel even mentioned that he could just use num.toString(2), but he wanted to show the conversion from decimal to binary. What I don't understand in your proposed code though is why you want to logically shift right by zero and then convert to binary.
@@sigmareaver680 Yes, and then reverse the Array and you got the numbers in the correct order.
IceCreeper28 You can ignore the triple shift operator, it is just there to prevent shifting negative numbers. That could give undesirable effects. It's up to you to use it if you intend to manage the binary representation of negative numbers (it's band dependant though, 32 bits ? yep, i just tried in the console).
4:52
"Function ..."
**writes something not even remotely resembling the word function**
Haha
Tijmf6 😂
I always type funciton! Function seems to be a difficult word to type! Maybe this is why in javascript we now have arrow functions!
"Shift.."
"ehi"
**backspace intensifies**
"ejot"
**backspace again**
You are SO the perfect nerd. Loving your uploads! Thanks!
he is so passionate about what he does..i want to be like him..😊😍
I know right! It's infectious, I love it.
Dude Dan the happiest shiftman.. your excitement is so contagious!
I was going to make a "clever" joke about Shiftman but you made it for me :(
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
please do challenge in Line algorithm like bresenhams line, midpoint line etc.
i love your entire video...
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
Thank you!!
I love your energy man, great explanation btw
"tijmf6 shiftBits() {
}"
Has a nice ring to it.
Ive added both kinda shifts by number of bits user specifies.
:D , man you really have developed my interest in coding again...
Like it when you cover the old school tricks!
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.
Mr SHIFTman you did some good bit of bit SHIFTing on this SHIFT.
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?
You got so excited at 13:35 :D you're great
We love you, Daniel!
do you think it would be hard to do a coding challenge without talking? hahah Love your videos btw! It's nice to hear you think everything out!
Thank you for this and #119. I hope to get good at programming. I figure that bit operations are important.
Hey, could you make a coding challenge for a pool game ? It would be cool :D
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
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.
Oh, okay. I forgot about you, the viewer! Auhauhauhaaua. (Daniel getting into the his own world of craziness and creativity)
You make me love Rainbows. Thanks that you beeing here
I had no idea JavaScript supported bitwise operators just like C
Interesting video as always 😁
Well Explained !
function DecToBinStr(value)
{
if(!value) return "0";
let output = "";
while(value > 0)
{
output = (value & 1) + output;
value >>= 1;
}
return output;
}
Just like shifting a base 10 number(Regular numbers we use as humans) either multiplies it or divides it by 10
You could have put the updating of the byte array right into the decimalToBinary for loop. Would have saved CPU cycles. This would of course require a rename of the decimalToBinary function. Maybe decimalToByteArray or so.
Decimal to binary is easy:
int n = 255
char[] byte = new char[8];
int c = 7;
while(n!=1){
char[c] = n%2 == 0 ? 1 : 0;
n /= 2;
c--;
}
( I dont know if this works 100%, but i have a code that uses the same logic ( n%2 ) that works fine )
Is it possible to request a topic be covered? If so, I'd really like help with concave polygon triangulation and collision detection. I haven't found good explanations online.
Please suggest here! github.com/CodingTrain/Rainbow-Topics/issues
Maybe, in the next codding challenge, you can try to rappresents atomic orbital shape using functions
I wanted to like this video- you seem very knowledgeable and can definitely explain things well.... the problem I found is that the presentation was a bit ‘scatter brain’.
The explanation jumps around too much and you reference things we don’t know about.. I’m already confused about *why* we need to binary shift but I only made it to 06:34 before I bailed.
This would be good as a first take...
5:42
Love it!
So you are Daniel Shiftman? :D
Now I watched the video ^^
New coding challenge: two swirling galaxies colliding... :D
5:44
I always do this when I'm explaining something to someone...And they just have no idea what I'm "oh"ing and "ah"ing about.
Wait, the next video isn't available?
Wait, this video was posted a few days ago?
*I caught up?!* :(
12:48 My eyes are burning for not using "%=" operator!
avi12
Oh look! the next Mark Zuckerberg 😂
you should mess around with hexadecimals while you're on the subject of bitwise stuff!
Subbed!!
Can you make half life 3 using any coding language ?
lazlo the boy It was a joke. Since the world is already waiting for HL3 since 2004. It has some kind of legendary status.
@@thebirdhasbeencharged opengl sucks :P
*tijmf6*
Ah yes, my favorite function name
Do it with any base (with a base slider) ;)
lol thats so cool!
Why are you using “let“ instead of “var“?
I talk about let and var in this video: ua-cam.com/video/q8SHaDQdul0/v-deo.html
💜💜💜
I have a question actually, how does p5 knows that the p5's native functions are called outside of draw or setup?
Because when p5 is imported the functions are made global, they can be accessed anywhere.
No, i mean when i use any function outside of draw() or setup() p5js gives me - "Did you just try to use p5.js's rect() function? If so, you may want to move it into your sketch's setup() function.
" - This Warning Message, but how does p5js knows that i am using it outside of setup() or draw()?
@@anuraghazra4772 you can create your own functions right? Well if you "create" a function that is part of p5 it will first try to treat it as your own function but then see it exists and realise that it should be in setup or draw. That's what I think.
@@anuraghazra4772 my guess would be because technically everything is being called via either the setup or draw, e.g show has rect/ellipse functions but it gets called from draw, maybe you're trying to call these functions in a different way? It'll give you a warning because I believe the draw function is supposed to know what is being drawn and external calls might mess with it
I dont know how they do it, but a possible way to do:
function onload()
{
//....
//otherstuff here
SETTINGUP = true;
setup();
SETTINGUP = false;
//otherstuff here
//..
}
function size()
{
if(SETTINGUP )
{
//...
}
else
console.debug("Warning message blah blah blah ..");
}
Anyway thats just my guess. (I didnt check if thats what they do).
0:01 Hello! Welcome to coding challenge, BITCH!! Shifting...
Trying to learn to code idea where to start
My beginner playlist (no prior knowledge required) is here: ua-cam.com/video/yPWkPOfnGsw/v-deo.html
You should release more videos with a premier before the actual release
Seventh!
Where is interesting stuff like ml5.js?
11:26 Or just: 2 ** i
ahaha you're so cool
holaaaa
Please make a Voronoi cells coding challenge!!
90 / 128 = 0r90?? What maths are you doing mate?
first!!!!
RIP now we will have horrible codebases where people bitshift instead of using / and *
more premiers
This seems like the mind of someone with ADHD, going all over the place. Good video though :)