703
edits
(Created page with "== Binary numbers and Variables == Any modern computer uses binary stored data. There are several ways to do that and there's are many different standards - however most of the ...") |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 70: | Line 70: | ||
So this seems all logical - however there's a small trap in using this representation. If all bits are zero everything is clear - the number 0000 0000 means positive and the number is zero. But 1000 0000 means negative - and also zero. This may cause problems (and in Fact there are problems likely to be related on this: The lidl cash system crashed when you bought items for an amount and then gave empty bottles so the resulting price should be zero. ) and so there are several definitions to have only one zero. Unfortunately this looks not very intuitive at first: | So this seems all logical - however there's a small trap in using this representation. If all bits are zero everything is clear - the number 0000 0000 means positive and the number is zero. But 1000 0000 means negative - and also zero. This may cause problems (and in Fact there are problems likely to be related on this: The lidl cash system crashed when you bought items for an amount and then gave empty bottles so the resulting price should be zero. ) and so there are several definitions to have only one zero. Unfortunately this looks not very intuitive at first: | ||
0111 1111 means 127 | 0111 1111 means 127<br> | ||
... | ...<br> | ||
0000 0010 means 2 | 0000 0010 means 2<br> | ||
0000 0001 means 1 | 0000 0001 means 1<br> | ||
0000 0000 means 0 | 0000 0000 means 0<br> | ||
1111 1111 means -1 | 1111 1111 means -1<br> | ||
1111 1110 means -2 | 1111 1110 means -2<br> | ||
1111 1101 means -3 | 1111 1101 means -3<br> | ||
... | ...<br> | ||
1000 0001 means -127 | 1000 0001 means -127<br> | ||
1000 0000 means -128 | 1000 0000 means -128<br> | ||
You don't have to remember this when programming simple things - however this may become important if you use bitwise operations. Important for now is only that the upper and lower limit of a signed int in Arduino differ by 1 bit. | You don't have to remember this when programming simple things - however this may become important if you use bitwise operations. Important for now is only that the upper and lower limit of a signed int in Arduino differ by 1 bit. | ||
| Line 96: | Line 96: | ||
Since you want to store several numbers you have to find a way to store keep them remembered. A common way to accomplish that is giving names to such a variable: | Since you want to store several numbers you have to find a way to store keep them remembered. A common way to accomplish that is giving names to such a variable: | ||
int myNumber; | int myNumber;<br> | ||
int anotherNumber; | int anotherNumber;<br> | ||
now Arduino knows that you are going to use 2 integers. Their content is now the value 0 - that's for convenience. | now Arduino knows that you are going to use 2 integers. Their content is now the value 0 - that's for convenience. | ||
| Line 103: | Line 103: | ||
after declaring the variables you can store something in them: | after declaring the variables you can store something in them: | ||
myNumber = 20; | myNumber = 20;<br> | ||
anotherNumber = 30; | anotherNumber = 30;<br> | ||
stores 20 and 30 in variables called myNumber and anotherNumber. | stores 20 and 30 in variables called myNumber and anotherNumber. | ||
| Line 110: | Line 110: | ||
Every time you need one of those numbers you can use myNumber and anotherNumber. Their values don't change until you order them to do so, for example by writing: | Every time you need one of those numbers you can use myNumber and anotherNumber. Their values don't change until you order them to do so, for example by writing: | ||
myNumber = 25; | myNumber = 25;<br> | ||
now the 20 is forgotten and the value of myNumber is 25. | now the 20 is forgotten and the value of myNumber is 25. | ||
| Line 143: | Line 143: | ||
now myNumber has the same value as anotherNumber and its value is printed. | now myNumber has the same value as anotherNumber and its value is printed. | ||
==== Testing roll over ==== | |||
<source lang="c"> | |||
void loop () { | |||
Serial.println(myNumber); | |||
myNumber = myNumber + 1; | |||
} | |||
</source> | |||
Each time the loop is started again the number is first sent to the computer and then increased by 1. | |||
Let the program run for a while and see what happens when the border of an integer variable is reached. (when reaching 32,767) the number is suddenly "wrapped over" to the most negative number possible: -32,768 and from there starts again countin towards 0 - reaching the limit at the upper and again and so on. This roll over can be used in programs to infinitely count - however since it is not obviously at what point the number will roll over it's no nice programming style. | |||
Now you can use the same program but use "unsiged int" instead int. Observe that now the number rolls over to 0 and not to a negative number. | |||
You can't only modify variables inside the loop function, but also in the setup fuction: | |||
<source lang="c"> | |||
unsigned int myNumber; | |||
void setup () { | |||
Serial.begin(9600); | |||
Serial.println(myNumber); | |||
myNumber = myNumber - 1; | |||
Serial.println(myNumber); | |||
} | |||
void loop () { | |||
} | |||
</source> | |||
Now everything happens in the setup function - so it happens only once. Then the program will continue in the loop but there it does nothing and nothing and nothing again infinitely. | |||
Notice the roll over also happens in both directions. | |||
edits