Arduino

Why the FUCK is this an infinite loop? I'm converting natural numbers to binary numbers. It displays the following:

0
1
10
10
...

instead of giving me the binary representation of 11 for 3. It works when I manually define numb t be 3 but breaks when I try to loop and make it count

Attached: killme.jpg (331x431, 33K)

You enter a while loop in which numb is always greater than one with no exit condition

C++ has a function for that, so why would you write your own?

Wrong.

You're modifying numb which is the iterator for your for loop like a dumbass.

When numb == 2 it gets floored in while loop into 1 which causes infinite loop. Btw that if in while is 100% useless. Arduino is just C so learn C

Because I'm trying to learn the language
Thanks, I'll check it out. I just realized that if in while is useless. I did this code in 15 minutes in MATLAB and it works perfectly, now I've spent 2 hours trying to make it work in Arduino

Line 10, you integer divide numb by 2. This line is first hit on the iteration when numb = 2. After line 10, numb is now 1. Numb goes through the rest of the iteration unchanged, then the numb++ in the for loop header puts numb back to 2. This process gets repeated again. The for loop’s break condition, numb >= 3, is never reached. This doesn’t happen on the first two iterations of the for loop because the while loop at lines 6-12 doesn’t run until numb >= 2. Fix this by using a dummy variable like a or c for the for loop counter in line 2, and immediately after line 2 write int numb = a; This copies the value of the for loop variable, which I’m assuming represents the number you’re converting to binary, so that you have a variable to manipulate in the body of the loop without affecting the loop variables.

Long story short, don’t fuck with the variable in the for header if you don’t know what you’re doing. Also, number your fucking lines.

low iq post

actually, turns out I need that if statement for whatever reason.

Fucking thank you, man, I got it working. And thank you for the advice.

Reinventing the wheel isn't always necessary.

Implementing a wheel helps you learn how it works, though.

Which you can do on paper instead, in less time, and without having to worry about code.

If this is a learning thing, get a good IDE and learn how single-stepping works.

>I'm converting natural numbers to binary numbers
>using mod 2

God damn CS majors are fucking retarded. Use numb&1 or numb^1 to mask out all the bits but the least significant. Then bitshift left by 1 bit, numb, to divide by 2. Also WHY DA FUCK ARE YOU USING FLOOR FOR INTEGER DIVISION! IT ALWAYS FLOORS ALWAYS.

Attached: 45b1cd5a4ba41598d65eb4dc722c66ac.png (343x383, 15K)

>left
*right

for(int num=0;num=1;
[math]~~~[/math]}
[math]~~~[/math]for(int i=3; i>=0; --i){
[math]~~~~~~[/math]Serial.print(bin[i]);
[math]~~~[/math]}
[math]~~~[/math]Serial.print(' ');
[math]~~~[/math]delay(200);
}

I haven't seen code this confused in quite a while.

int Binarymod[4] = {0,0,0,0};
for (int numb = 0; numb < 16; numb++) {
Binarymod[0] = numb & 1;
Binarymod[1] = (numb >> 1) & 1;
Binarymod[2] = (numb >> 2) & 1;
Binarymod[3] = (numb >> 3) & 1;

for (int i = 3; i >= 0; i--) {
Serial.print(Binarymod[i]);
}
Serial.print(' ');
delay(200);
}

Attached: think.jpg (400x400, 23K)

what the fuck is this code

?

This, most people fail to meet deadlines because they tend to waste time like OP.

Most people drive a car without knowing exactly how it works, same applies to functions in most cases, just grasp the basic concept and think for a little bit how it probably does it's thing (at least when it comes to stupid functions like this one).

Better use your time on polishing your math skills so you don't get looped like that again.

>Why the FUCK is this an infinite loop?
Are you retarded?

// numb is 0 the second ineration of the for loop
while (numb > 1) {
// ...
if (numb == 1) {
numb = 0; // numb is 0
}
numb = floor(numb/2); // numb is 0
}

mods broke it

>Using int for binary values

Holy shit, you made space for 64 bits of data in memory when you only needed 4.
What is resource management??