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
You enter a while loop in which numb is always greater than one with no exit condition
Blake Smith
C++ has a function for that, so why would you write your own?
Cooper Ortiz
Wrong.
You're modifying numb which is the iterator for your for loop like a dumbass.
Jeremiah Gonzalez
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
Adam Perez
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
Jackson Gonzalez
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.
Zachary White
low iq post
Gavin Lopez
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.
Andrew Fisher
Reinventing the wheel isn't always necessary.
Oliver Jenkins
Implementing a wheel helps you learn how it works, though.
Noah Richardson
Which you can do on paper instead, in less time, and without having to worry about code.
Ryder Morgan
If this is a learning thing, get a good IDE and learn how single-stepping works.
Joshua Lopez
>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.
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.
Justin Gutierrez
>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 }
John Collins
mods broke it
Nathan Young
>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??