Working with digital values

MobiFlight uses in some places binary and hexadecimal number formats, e.g. to read and write certain offsets.

The tutorial explains how values are represented digitally in the computer, what different number formats there are and how it helps to create and better understand your own configurations in MobiFlight.

The tutorial was created by stephanho - Thank you!

In today's world, digital - no matter in which area - has become indispensable.
But how can you explain digital to someone who has little or no knowledge of digital technology?

This requires first of all the basics, that is, the fundamental knowledge on which everything else builds.

Number Systems

These basics include first of all number systems, which are simply necessary for working in the field of computers in order to be able to develop an understanding of certain related things.

First we want to have a look at the dual number system, also binary number system, and the hexadecimal number system. Both number systems are seen over and over again when working with Mobiflight.

The dual number system or the hexadecimal number system is mathematically only one number system like any other. The best known number system is the decimal number system and consists of the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

With these numbers we can write all the numbers and even do calculations with them.

123 + 456 = 579
157 – 102 = 55
17 x 19 = 323
618 ÷ 103 = 6

This is not foreign to us at all. These numbers from the decimal number system are based on the number base 10.

Actually, you can also write 123 differently:

1x102 + 1x101 + 3x100=123
100   +  20   +   3  =123

Please remember that every number bt the power of 0, or n0, is always 1. 100 is always 1, as well as 20 or 160 or or.

The binary number system / dual number system

Where to start now? That would be the bit.

So, the bit is the smallest digital unit out there. A bit can only accept two values, namely 0 and 1. Like a light bulb: "on" or "off" - ok, it breaks, but then it would be "off" again Wink

On this the whole computer technology is based? Exactly. Digital technology is as simple as it is ingenious. I explain that now:

Let's start with the units.

  • 1 bit is the smallest unit. The next larger unit is a crumb, which is 2 bits (hardly used today). This is followed by 
  • the nibble, these are 2 crumbs or 4 bits. Nibble is also called Halfbyte. This is followed by
  • the byte, which is 8 bits or 4 crumbs or 2 nibbles or 2 half bytes.
  • 2 bytes are a word, ie 16 bits, 4 bytes are called Double Word (32 bits) and 8 bytes are called Quadrouple Word = 64 bits

A word, a data word is the basic agreement size for a computer. The size of this data unit in bits is referred to as word width or bus width.

Basically, this bus has nothing to do with public transport, although a bus (in the digital sense) is certainly used to transport data. Do you already recognize the connections between bit, word and bus? So much for now.

Let's get back to the dual number system.

The dual number system is also called binary number system. Dual comes from the Latin dualis and means something like "containing two". Binary is also derived from the Latin "bini" and means something like "two each" or "bina" for "double" or "pairwise". In the binary number system, only the numbers 0 and 1 exist.

How to calculate in the binary system?

Just like in any other number system.

In the decimal system we expect

510 + 510 = 1010

The small 10 after the number should clarify that we are moving in the decimal number system.

5 + 5 equals 10 and we write the 0 on the right and remember the 1 for the overflow of the right number. Since no further numbers are added to the left, we write down the number noted there, so the 1st 1 followed by the 0 is read as 10.

It's the same with the binary system. Here each number is assigned a small 2 to make it clear that we are working with numbers in the binary number system - base 2.

     12
+   12
= 102

As you can see, without stating the number base, the result would be identical. Only one reads the result here not as 10 (ten) but as 1-0 (one - zero).
1 + 1 is 10, because 1 + 1 - this is already an overflow and we write down the 0 on the right and remember the overflow. Since nothing is added on the left, we write the overflow directly. So 1 - 0

Above, at 5 + 5, nobody will question the result. We are so familar with base 10 arithmetics. But how does that look like in the binary system?
Above we calculate 5 x 100 + 5 x 100 = 5 x 1 + 5 x 1 = 10, or 1 x 101 + 0 x 100.

When adding our two 'ones', we do not do anything else, just with a different base of numbers

1 x 20 + 1 x 20 = 1 x 1 + 1 x1 = 10 or 1 x 21 + 0 x 20.In the decimal result this would be 2. So you can convert any number from any number system to any number system. As mentioned above, we feel at home in the decimal system. Anyone who has understood the principle after reading this now feels native in any number system ;)

An example from the world of Mobiflight & Co:
Since Jeehell needs a certain bit, let's say 6th from any offset.

Now the value 268 is read from the simulation for this offset. This value is decimal.
OK, just started the Windows computer, switched to programmers and in the DEC mode, the 268 entered, switched to BIN mode.

Result:
100001100

or better legibility in Nibbles:
0001 0000 1100

As in the decimal system, leading zeros are usually not shown, they should be written here for better understanding and readability.

The searched sixth bit is therefore 0.
But caution is needed. We are used to counting 1, 2, 3, 4 .... Not so in digital technology. This starts at 0 (zero). So: 0, 1, 2, 3, 4 ...

So if I'm talking about the 6th bit, that's the 7th number from the right!
This has something to do with the value and thus describes the power of the number base.

Bit 0 in digital technology is the equivalent of 20, the 1st bit to 21. The 6th bit would thus be 26 associated. A way to remember it easily.

Why potencies?

Basis:

1 x 20 = 1
1 x 21 = 2
1 x 22 = 2x2 = 4
1 x 23 = 2x2x2 = 8
1 x 24 = 2x2x2x2 = 16

So, if I look at 4 contiguous bits, it may look like this:

0 0 0 0

Binary now the question arises, how many different presentation options does you have with 4 bits?

Let's take a look at this:

0 0 0 02 = 010
0 0 0 1 = 1
0 0 1 0 = 2
0 0 1 1 = 3
0 1 0 0 = 4
0 1 0 1 = 5
0 1 1 0 = 6
0 1 1 1 = 7
1 0 0 0 = 8
1 0 0 1 = 9
1 0 1 0 = 10
1 0 1 1 = 11
1 1 0 0 = 12
1 1 0 1 = 13
1 1 1 0 = 14
1 1 1 1 = 15


There are at 23 so 4 bits 16 possibilities of a different representation possible.

You can also say 24 (2x2x2x2 = 16) possibilities, where decimal is the highest representable number in this example 24 -1. So if I have 8 bits, I have 28 display options (2x2x2x2x2x2x2x2 = 256) and the highest decimal number
is 28-1 = 256-1 = 255. More on that later.

There is another way:
Take the 268 from the above offset and divide by 2 until the 1 is divided by 2 at the end.

It will look like that:
268 ÷ 2 = 134 remainder 0
134 ÷ 2 = 67 remainder 0
67 ÷ 2 = 33 remainder 1
33 ÷ 2 = 16 remainder 1
16 ÷ 2 = 8 remainder 0
8 ÷ 2 = 4 remainder 0
4 ÷ 2 = 2 remainder 0
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1

Now you write the numbers after the rest from left to right next to each other in the reverse order, how to get the residual values.

The last residual value comes from the calculation 1 ÷ 2 = 0 remainder 1, so our result:

1 0000 1100The numbers from the rest thus from bottom to top convert from left to right.

Bit #6 is the 3rd zero in the 2nd nibble or the 7th number of the 1st byte.
Bits are therefore counted from right to left. The right one, ie the least significant bit, is also called Least Significant
Bit (LSB), the most significant bit is called Most Significant Bit (MSB).
This is also going backwards. It starts with the MSB

1 x 28 + 0x27 + 0x26 + 0x25 + 0x24 + 1x23 + 1x22 + 0x21 + 0x20

256      128     64       32       16         8        4          2         1I've written the weights of the bits in the decimal system below. Now you just have to add the values ​​that contain a binary 1:

256 + 8 + 4 = 268If I want to put the 6th bit in the offset, I have to add the decimal value of 26 = 64 to the existing value

268 + 64 = 332Check if bit # 6 is set:

332 ÷ 2 = 166 remainder 0
166 ÷ 2 = 83 remainder 0
83 ÷ 2 = 41 remainder 1
41 ÷ 2 = 20 remainder 1
20 ÷ 2 = 10 remainder 0
10 ÷ 2 = 5 remainder 0
5 ÷ 2 = 2 remainder 1
2 ÷ 2 = 1 remainder 0
1 ÷ 2 = 0 remainder 1
Binary: 1 0100 1100

Please, bit 6 is set.

Of course, this is much faster and easier with the Windows machine. But with this kind you are not a push-button housekeeper, but you also know WHY things are like that. Congratulations!

The Hexadecimal System

The next number system that one should know of is the hexadecimal system. "Hex" has its origin in Greek hexa which means six and decem in Latin stands for ten. There is another similar term sedecim (Latin: sixteen), which pretty much means the same thing.

The hexadecimal system consists of 16 different "digits". It contains the digits we know from 0 to 9 and then the letters A, B, C, D, E and F, the first six letters of our alphabet.

Hexadecimal numbers are written as usual with the base number 16, ie A3CE16 or A3CEhex or as 0xA3CE.

The last way to write the hex number is already known to us from Mobiflight and you see, now the circle is closing.

Again, the conversion into other number systems according to the known pattern:

A = 10, B = 11, C = 13, E = 14 and F = 15Thus

HEX: 10x16+ 3x162 + 12x161 + 14x160
DEC: 40960 +    768  +      192 + 14  =  41934

It's also binary:

BIN: 1010 0011 1100 1110
(       A       3       C      E     )
calculated:

1 x215 + 1x213 + 1x29 + 1x28 + 1x27 + 1x26 + 1x23 + 1x22 + 1x21
32768  + 8192  +  512 +  256  + 128  +  64    +  8     +  4     + 2     = 41934
It can be seen here that the numbers can be easily converted from one system to another and the math is still correct;)

Here is a comparison of the previously introduced number systems:

DEC BIN    HEX
00     0000  0
01     0001  1
02     0010  2
03     0011  3
04     0100  4
05     0101  5
06     0110  6
07     0111  7
08     1000  8
09     1001  9
10     1010  A
11     1011  B
12     1100  C
13     1101  D
14     1110  E
15     1111  F

The hexadecimal system is thus a base 16 number system. The comparison shows that the systems are not the same, but are still compatible with each other.

Let's take the number 268 from the top again and have a look at it

268  0001 0000 1100  10C

This is done by setting bit #6

332  0001 0100 1100  14C
10C can also be converted into the decimal system:

1 x 162 + 4x161 + 12x160 = 256 + 12 = 268

or

1 x 16+ 4 x 161 + 12 x 160 = 256 + 64 + 12 = 332

You see, the math is correct and you can convert any number from any number system to another number system.

So if you have many bits, it makes sense to convert them into a hexadecimal system. Please note that you start on the right and fill with zeros at the last digit.

An example:

  • 1001 1111 1011 0011 in the binary system
  • 9FB3 in hexadecimal system or
  • 40883 in decimal system.

If you like, you can check it with the above methods.

Data types

When working with digital numbers you can not avoid dealing with number types. This is related to the C programming language, where these data types are used by the programmer to efficiently use the memory.

Which data types are there?

Boolean (Byte)

The smallest unit data type is boolean. This data type occupies one bit of the memory. After what we've learned so far, bits can only take state 0 or 1. The states are also called TRUE (1) or FALSE (0).

Integer (INT)

Integers are integers that can take values ​​in the range of -32767 to +32768. This data type is stored in a Word. A word is 2 bytes, which is 16 bits of memory.


Long (LONG)

These are also integers, as well as integers, only to cover a wider range, namely from        -2,147,483,648 to 2,147,483,647. Long's are Double Word, so they are twice the length of Integer, ie 4 bytes or 32 bits.

Float (FLOAT)

This means floating-point numbers, ie numbers with digits after the decimal or decimal place. The range of numbers goes from -3.4028235E+38 to 3.4028235E+38, is also a double word and has 4 bytes or 32 bits in length.

Double (DOUBLE)

Like FLOAT, only twice the length, so 2x double word, 8 bytes or 64 bits

String (STR)

For strings, their length is always specified (STR [12]).

Signed/ Unsigned

By SIGNED is meant that the data type is signed. Basically, all previous data types are signed, that is, they are signed. You need this to be able to represent negative numbers.

Unsigned are unsigned numbers. Normally, the sign is passed by the most significant bit (MSB). If the MSB is 1, it is a negative number, if it is 0, the number is positive. Unsigned numbers have the advantage that they are 1 bit longer.

For (signed) INT numbers this means that they are only 15 bits long plus the sign bit (ie from -32767 to 32768) and (unsigned) INT numbers are 16 bits long and range from 0 to 131071).

We already know a problem area of ​​the numbers signed / unsigned from Mobiflight. It's the numerical value of the right single mouse click: 2,147,483,648

This numerical value is exactly 1 above the maximum range of the signed long INT range.

Binary this area goes to 0111 1111 1111 1111 1111 1111 1111 1111,

These are 2,147,483,647. As you can see, the MSB is 0, which is why the number is considered a positive number. If 1 is added, we have

1000 0000 0000 0000 0000 0000 0000 0000

If the sign is negative and the rest of all bits is 0. So -0. 0, mathematically speaking, can not be negative, so an error is output.

If, however, the 2.147.483.648 is preceded by a minus (-), then it is almost the smallest representable number of -2,147,483,648, the result is -2,147,483,648, which is binary 1000 0000 0000 0000 0000 0000 0000 0000 Mobiflight can take this value.

Mobiflight now transfers this value to the Sim, which is unsigned there, and thus the sign of Mobiflight in the simulator becomes a value without sign and thus the single right mouse click is detected and executed. A small, but valid, trick.

Bit Logic

One more useful thing can be done with this number system:

Filter, modify and mask.

Many have already seen masking who worked in Mobiflight in ConfigWizard. Usually here stands 0xFF or 0xFFFF depending on the number of bytes set above.

Already we are in logical operations. Logical operations are part of Boolean algebra, a branch of mathematics.

Everyone has heard the logical operators before:

AND
OR
NOT

Nothing more than a tabulation of the truth value of a logical statement is required for these operators.

Truth table for an AND link

a b A
0 0 0
0 1 0
1 0 0
1 1 1

The result of this truth table for the logical AND is that both a and b must have state 1, so that at the output A there is also a 1.
Transfer to Mobiflight: Only if Pin1 and Pin2 are at GND will this cause an action.

For a logical OR, this looks similar:

a b A
0 0 0
1 0 1
0 1 1
1 1 1

So here, only input a or b or both must be 1 for an event to be triggered.

Missing the NOT, also called inverter.
a A
0 1
1 0

So you can make from an AND a NAND, a NOT-AND and from an OR a NOR or NOT-OR

There are other logical operators, but if you want to deepen that, you should be more concerned with Boolean algebra. For our purposes this should be enough.

Again to mask values ​​in Mobiflight.

The masking value in Mobiflight is always linked to the current value of the simulation AND. Since Mobiflight always links to 0xFF AND by default, it looks like this:

Take a value from the offset 0x03FA (COM 1 stby frequency). For example, the value that the simulator provides is 2855 for the 128.55 MHz frequency.

Defined here is a value of integer and 2 bytes in length, ie 16 bits

2850 is binary 0000 1011 0010 0010

But here comes a peculiarity to bear: Here is the hook set in BCD mode.

What does that mean?

BCD stands for Binary Coded Decimal.

This converts the binary number directly into a decimal number.

The value supplied by FSUIPC is defined as 2-byte integer, ie 16-bit integer.
Thus, each of the 4 digits is assigned as a binary number of 4 bits each, so the 2 comes in the bits 15, 14, 13 and 12, the 8 in the bits 11, 10, 9 and 8, the 5 in the bits 7, 6 , 5 and 4 and the last 5 in the bits 3, 2, 1 and 0

Binary the frequency (1) 2855 looks like this:

0010 1000 0101 0101

This is in hexadecimal

2      8      5      5

Is something noticeable?

If a binary value (which is also a hexadecimal value) is BCD-coded, then the HEX value corresponds exactly to the decimal counterpart.

This value is linked to FFFF AND. You can also resolve FFFF in binary so that it becomes clear:

1111 1111 1111 1111
F      F       F      F

0010 1000 0101 0101
1111 1111 1111 1111

An AND operation states according to the truth table that, as a result, a 1 only comes out if both inputs supply an 1.
In this case, it is easy because the upper value is also delivered as a result.

0010 1000 0101 0101
1111 1111 1111 1111
0010 1000 0101 0101

If I want to always set the last nibble to 0, I have to change the masking value:

0010 1000 0101 0101
1111 1111 1111 0000
F      F       F      0

The result would be

0010 1000 0101 0000

or

2       8      5       0

The last digit of my number would ALWAYS be a NULL in this masking.

Whether this action would be useful, I leave it out of the question. But it shows what possibilities can arise if you master this number game.

Note: A value that is masked with F, FF, FFFF always has the input value in the result.

If you want to set a bit in a byte, I need to know where the bit is.
For this one must remember only the following values ​​from bits 0 to 7: 1, 2, 4, 8, 16, 32, 64 and 128. If several bits are to be set at the same time, the values ​​of the bits must be added.

An example:
Let's look at the offset 0x0D0C in the FSX (Lights, a switch for each one)

0 = navigation
1 = beacon
2 = Landing
3 = taxi
4 = strobes
5 = instruments
6 = Recognition
7 = Wing

To turn off all lights, the offset is simply set to 0.

Structure of the bits for masking:

W   R    I   S  T  L B N
7    6    5   4  3  2 1  0
128 64 32 16 8  4 2  1

We had just switched off all the lights with the 0. This sets all bits to 0.

The reverse is also possible by setting all bits to 1. Earlier, I explained how to get the number of different representations.

We have 8 bits (0 - 7). Thus we have 28 - 1 possibilities, thus 255.
By writing the offset at 255, all the lights go on.

For example, if I want to know if the beacon is on, I need to mask bit 1.
To do this, the masking is set to 0x02 instead of 0xFF. We remember: Masking always links AND.

0010 0011 is for example the value of the offset. Decimal it is 35. If I masked now with 0xFF, I would get only as result 35 again. Therefore, it is masked with 0x02. Because

0010 0011 masked with
0000 0010 results
0000 0010 which gives 2 decimal.

I know that the beacon is ON.

If 0x04 had been masked to check the landing light, the result would be 0:

0010 0011 masked with
0000 0100 results
0000 0000

The landing light is turned off. To turn it on, you would have to read the offset and enter it in the Transform window $ + 4. Because you would just write the 4 in the offset, all the other lights would go out and only the landing lights are turned on. In this respect you must add 4 to the existing value.

You can do this per configuration line only for each request individually. So you need 8 configurations for this offset.

Evaluate and influence of values

Mobiflight has the ability to affect values ​​via the Transform line.

This is done with the function IF (Condition, THEN, ELSE)

THEN and ELSE as such are not used. For the clip is available. The 1st parenthesis value is the condition to be evaluated. The comma separates the THEN area from this. This is executed when the condition is met. The next area after the second comma contains the ELSE area. This is executed if the condition is not fulfilled and THEN is not executed. Optionally, the ELSE area can be left empty. But it makes sense to enter $ here.

Example:

IF($>359,0,$)This line reads as follows:

If (the Sim value is greater than 359, then 0, otherwise the Sim value)

You can also nest the IF conditions. In order to stay with the compass function, it should now be checked whether the value is less than 0.

IF($>359,0,IF($<0,359,$))The first IF now checks whether the Sim value is greater than 359, if so, 0 should be output. However, if this is not the case, a new IF condition comes in the ELSE area, which checks whether the Sim value is less than 0. If the value is less than 0 then 359 should be displayed. So ELSE will be executed if the value is not greater than 359. In fact, there is an IF condition here again.

Assuming that the Sim value is 255, neither the 1st nor the 2nd IF condition applies, because the value is not greater than 359 and not less than 0. Thus, only the 2nd ELSE condition remains and it will the sim value is output. Important here is the parenthesis!

Calculation rules in MobiFlight

Mobiflight used for working out prerequisites and transformation rules.

These are

+ - PLUS - everyone knows that 1 + 1 = 2
- - MINUS - 6-4 = 2
* - multiplication - 4 * 6 = 24
/ - DIVIDED - Division - 12/3 = 4
> - GREATER THAN - 6 is greater than 5, 6> 5
<- LESS THAN - 1 is less than 7, 1 <7
>= - GREATER SAME - x> = 5, x is greater than or equal to 5
<= - LITTLE SAME - x <= 5, x is less than or equal to 5
!= - NOT equal - x! = 5, x is not equal to 5
% = MODULO calculation - $%500 - 1000%500 = 0, 999%500 = 499
Furthermore you can influence calculations with brackets. Here is the mathematical principle: What is in parenthesis, is calculated first.

9 + 4 * 3 is mathematically correct 21 - here: point before line calculation (remember?)

(9 + 4) * 3 is also mathematically correct 39 - here, what is in parenthesis is calculated first. So take care with your own formulas!

Conclusion

You have now learned about number systems, data formats and value manipulation and calculation rules. This does not just apply to Mobiflight.

There is plenty of information on the internet for those who want to go deeper into the subject matter.

I hope to have eliminated a lot of question marks and you can now handle Mobiflight even better. It would please me.

StephanHo for Mobiflight in February 2019