703
edits
No edit summary |
No edit summary |
||
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. |
edits