IFD:GrundlagenElektronik2011/BasicsCourse

From Medien Wiki

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 are the same in all languages and the differences aren't that important for now.

Integer numbers

An integer number is a number like 1 2 3 4 or -1 -2 -3 -4 and 0 is included. An example for a number which is not integer: 1.6 or 1.5327

A comuter uses only the values 1 and 0 (or HIGH and LOW or True and false) as numbers. This is called "binary". We are used to have 10 different symbols - this is called decimal (from the latin word decimalis which means ten).

Any positive integer number can easily be converted from decimal to binary representation - and vice verse. At first we will start counting - from 0 to 10 in binary:

0 is 0
1 is 1
2 is 10
3 is 11
4 is 100
5 is 101
...
15 is 1111
16 is 10000
17 is 10001
18 is 10010
...
254 is 1111 1110
255 is 1111 1111
(the space is only there for readability)

Each digit represents a fixed value like we are already familiar with from decimal numbers - here for example the decimal number 1367501 can be composed of digits representing 1 10 100 1000 and so forth:

1367501 is the sum of the following:
1 x 1000000
3 x 100000
6 x 10000
7 x 1000
5 x 100
0 x 10
1 x 1

For binary numbers it is similar - the number 101101 can be decomposed to:

1 x 100000 = 1 x 32 = 1 x 25
0 x 10000 = 0 x 16 = 0 x 24
1 x 1000 = 1 x 8 = 1 x 23
1 x 100 = 1 x 4 = 1 x 22
0 x 10 = 0 x 2 = 0 x 21
1 x 1 = 1 x 1 = 1 x 20

you may have noticed the digits from right to left have the values 1, 2, 4, 8, 16, ... - each twice as much as the previous number.

as a small excercise convert these numbers to decimal numbers:

1001
0010
0101000

Each digit (one or zero) is called one "bit" so the number 10101010 consists of 8 bits. What is the maximum number you can describe by 8 bits in the way above described? what about a 10 bit number?

8 bit numbers are 1 byte large. On computers one byte is the mostly widely used groups of bits to store data. Larger groups of several bytes are very common too: 16bit (2 byte) or 24 or 32 bit - or more.

MSB and LSB

MSB means the "most significant bit" - so this is the bit which has the most influence on the size of a number. From left to right the numbers are decreasing in size - so the leftmost bit in the above example was the MSB. The LSB is the least significant bit - this is the rightmost bit. For now this is not important - but it will become important in more advanced programming and interfacing digital hardware (for example of if a device sends bits it may start with the MSB or with the LSB and you have to know that in order to decode a stream of bits).

positive and negative numbers

The above decimal numbers are only positive numbers. To save negative numbers on a computer one bit is needed to decide if a number is positive or negative. For example if you have only 8 bits available for storage you have to use one bit to indicate whether a number is positive or negative. So only 7 bits remain for the actual number. The standard for signed numbers is using the first bit for the sign and the remaining parts for a number. When the sign bit is 0 the number is positive when it's 1 the number is negative.

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
...
0000 0010 means 2
0000 0001 means 1
0000 0000 means 0
1111 1111 means -1
1111 1110 means -2
1111 1101 means -3
...
1000 0001 means -127
1000 0000 means -128

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 should now know the difference between a signed and an unsigned variable of the same bit size.

Variables in practice

On Arduino and other programming languages you have differently sized numbers (called variables) - to store data in them.

The keyword 'int' indicates an integer number. In Arduino it is a signed number of 16 bits.

Find out the approximate possible positive and negative numbers you can use with it. (Use a calcutator or the Arduino reference to find these limits).

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 anotherNumber;

now Arduino knows that you are going to use 2 integers. Their content is now the value 0 - that's for convenience.

after declaring the variables you can store something in them:

myNumber = 20;
anotherNumber = 30;

stores 20 and 30 in variables called myNumber and anotherNumber.

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;

now the 20 is forgotten and the value of myNumber is 25.

Since all this is hidden inside the arduino Microcontroller and you can't see the number inside it is necessary to have a method of seeing those variables.

The following sketch sets up a connection from the Arduino board to your computer in "setup" and then repeatedly sends the value of myVariable to the computer. It will do so infinitely.

int myVariable
int anotherVariable

myNumber = 20;
anotherNumber = 30;

void setup () {
  Serial.begin(9600);
}

void loop () {
  Serial.println(myNumber);
}


Variables can be modified in several ways - for simplicity we only use one method at first - later I will list several ways you will stumble across. We want our numbers to be modified so it doesn't print 20 all the time.

void loop () {
  Serial.println(myNumber);
  myNumber = anotherNumber;
}

now myNumber has the same value as anotherNumber and its value is printed.


Testing roll over

void loop () {
  Serial.println(myNumber);
  myNumber = myNumber + 1;
}

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:

unsigned int myNumber;

void setup () {
  Serial.begin(9600);
  Serial.println(myNumber);
  myNumber = myNumber - 1;
  Serial.println(myNumber);
}

void loop () {  
}

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.