Part 1 — Analog 2’s Offset Converter
Some of the logic symbols in Crestron SIMPL can definitely be confusing for beginners! There’s a help file for all of them built right inside of SIMPL, but personally I still found many of them difficult to understand.
For example, let’s look at the very first logic symbol in the list…
now if you are brand new to Crestron programming, and you read this help file page, and you knew straight away what this logic symbol does, then fair play to you, because this confused the hell out of me when I first looked at it!
So here’s my own attempt to simplify these symbols for anyone else who finds some/many of them confusing!
Analog 2’s Offset Converter
Before we can even begin to understand this logic symbol, we need to understand what signed and unsigned integers are, as this is what this particular logic symbol impacts.
Unsigned integers are always positive numbers. Why? Well because they don’t have a sign in front of them, they are literally “unsigned”!
In contrast, “signed” integers DO have a sign in front of them, indicating if they are positive or negative. They are EXPLICIT in indicating whether the number is positive or negative.
So for example, -5 is a signed integer….. +12 is a signed integer….. however 7 is an “unsigned” integer…..it can be implied that this is a positive number because there’s no sign, but it’s not EXPLICTLY stated as being positive, therefore it cannot be a signed integer.
Ok now that we’ve cleared up the difference between signed and unsigned, we now need to understand the difference between unsigned integers and signed integers in the world of Crestron programming.
Unsigned integers in Crestron can be useful for things like volume levels, channel numbers, or counts, where it wouldn’t make sense to have a negative value. The range of unsigned values is from to 65,535.
The reason for this specific number of 65,535 is due to Crestron unsigned integers being represented as a 16-bit value.
Therefore the value for an unsigned integer can be anything from:
0000 0000 0000 0000 — which is 0
to
1111 1111 1111 1111 — which is 65,535
The reason is that each “bit” working from right to left, is worth double the previous “bit”
so this:
0000 0000 0000 0001 is equal to the value of 1
0000 0000 0000 0010 is equal to the value of 2
0000 0000 0000 0100 is equal to the value of 4
0000 0000 0000 1000 is equal to the value of 8
0000 0000 0001 0000 is equal to the value of 16
and so on…
And you can add these up…
0000 0000 0000 0011 is equal to the value of 3
0000 0000 0000 0111 is equal to the value of 7
0000 0000 1000 0100 is equal to the value of 132
1111 1111 1111 1111 is equal to the value of 65,535!
32768 + 16,384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 65,535
So this is why the range of unsigned integers for Crestron programming can range from 0 to 65,535.
Now as far as signed integers are concerned, we can have positive or negative numbers….however we still only have 16-bits to store the value…. so therefore where still have a range of 65,535 in TOTAL, however have the numbers are allocated to the negative side…. so therefore signed integers range from:
-32,768
to
+32,767
Now if you’re wondering why the range isn’t from -32,768 to +32,768 that’s a good question! It’s because we need to reserve a single bit to allow us to be able to explicitly state whether the number is a positive number or a negative number…. after all, how can we define if it should be a — or a + !
The bit that we reserve for this is known as the “most significant bit” or MSB. It’s always the value to the extreme left, and as you may have guessed, the “least significant bit” is the one to the extreme right.
So if we “reserve” this most significant bit to determine if the signed integer is positive or negative, then we can quickly see what the number will be.
So for example, the bit value for -32,768 is as follows:
1000 0000 0000 0000
but wait a second….. this is also the number for 32,768?
So how does a processor know if the number is signed or unsigned? How does it know if this number should be 32,768 or -32,768?
We tell it!
The programmer has to tell the processor whether to treat a number as signed or unsigned through the code (by specifying data types). For example:
unsigned int myVar; // This is unsigned.
int myVar; // This is signed - by default in many languages.
One last thing before we actually get to explaining how the logic symbol works!
Two’s complement!
If you’ve never heard of this phrase before, it’s a clever way of expressing negative numbers in binary.
if we take the number -1 in binary, it’s actually represented as this:
1111 1111 1111 1111
now you might be thinking, this is 65,535! It’s easy to understand why you might think this….and in fact, if the integer was UNSIGNED then it would indeed be 65,535….. however as a SIGNED integer, this represents -1, and it’s all because of this thing called “two’s complement”
So the way it works is that every signed integer that begins with a “most significant bit (MSB) of 1 will always be a negative number.
So this as an unsigned integer….
1000 0000 0000 0000 is 32,768
but declared as a signed integer….
1000 0000 0000 0000 is -32,768
It’s because the MSB on the left is a 1….. whenever a binary number that is a SIGNED integer has an MSB of 1, then it’s a negative number.
However that still doesn’t explain HOW it’s expressed as a negative number in binary. What actually happens is that something called “two’s complement” is applied, which is a two-step process. I’ll go over it in more detail shortly, but for now, what you need to know is that with two’s complement, you essentially invert (flip) all the bits, with the exception of the “most significant bit”, which is the value to the extreme left, and is used to determine if the number is negative or positive, and then add 1…and that will give you the negative value.
Let’s apply this two-step process to the number….
so this number….
1000 0000 0000 0000
let’s first invert (flip) all the bits, ignoring the MSB, which is set to 1, which as a signed integer always tells us that the number should be negative:
(1) 111 1111 1111 1111
which equals….
16,384 + 8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 32,767
Now let’s do the second part which is to add 1, which makes it 32,768
So what is 32,768 expressed in binary?
1000 0000 0000 0000 !
So we’ve inverted all the bits, and then added 1…. we’ve applied the “two’s complement process” and now we have a value of 1000 0000 0000 0000 which is -32,768 as a signed integer!
To ensure this really makes sense….let’s look at this number as a SIGNED integer….
1100 0000 0000 0000
This is actually -16,384
Do you recall how you’d express an unsigned integer of 16,384 in binary?
Let’s apply two’s complement….
First step is to invert all the bits, but ignore the MSB
(1)011 1111 1111 1111
which is essentially this….
8192 + 4096 + 2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 16,383
Now let’s add 1 to make it 16,384
and what is this in binary?
0111 1111 1111 1111
but our MSB was set to 1, so therefore we know this signed number should be -16,384 and not +16,384
But if you compare the two above, you’ll see it’s the MSB that is the key difference….it’s determining that the value is negative!
So what about this number as a signed integer?
1110 0000 0000 0000
If you thought it was -8192 then well done!
Applying two’s complement….
invert all the numbers, excluding the MSB…
(1)001 1111 1111 1111
and then add 1….
010 0000 0000 0000
which is 8192, but because the MSB was a 1 and it’s a signed integer, we know the number should be -8192
So what would this number be?
1111 1111 1111 1111
let’s apply two’s complement….
first invert all the bits, excluding the MSB….
(1)000 0000 0000 0000
now let’s add 1
000 0000 0000 0001
if it was declared as a signed integer (MSB was 1) then it must be -1 !
so therefore 1111 1111 1111 1111 as a signed integer equals -1!
In terms of the phrase “two’s complement”, it’s not that important to know why it’s called this, it’s more important to know how values are negative if the number was declared as a signed integer and the MSB is a 1
However for completeness, it’s called “two’s complement” because of the process involved in creating the negative counterpart of a positive binary number, which involves complementing the number (inverting the bits) and then adding 1 to the result.
Ok so finally, with all this knowledge, we’re ready to understand the logic symbol!!
Analog 2’s Offset Converter
The logic symbol works, by taking an analogue value as an input, and then converting this number to
The input value was 6, and the output value was 32,774
why?
because this logic symbol flips the MSB….
so 6 in binary is:
0000 0000 0000 0110
Let’s flip the MSB…
1000 00000 0000 0110
This now equals 32,774
Now that we understand what actually happened, the help file starts to make more sense!
It’s receiving a value, then flipping the MSB and then outputting a new “signed” integer.
65526 in binary is:
1111 1111 1111 0110
if we flip the MSB we get:
0111 1111 1111 0110
which is:
32,758
Ok so now we know what it does, how might it be useful?
Well as an example, many external devices such as sensors or equipment might use two’s complement for reporting values, especially for signed data like temperature (which could be both positive or negative). When Crestron receives this data, it might need to convert the two’s complement value into standard signed notation for further processing or display on touch panels.
For example, a temperature sensor might report values in two’s complement:
- A temperature of 25°C might be represented as 0000 0000 0001 1001 (which is 25 in two’s complement).
- However a temperature of -25°C might be represented as 1111 1111 1110 0111.
If you’re controlling an HVAC system or logging temperature data, you need to ensure that the negative temperature is correctly handled. The Analog 2’s Offset Converter would be useful here to flip the sign bit and interpret the two’s complement number as -25°C correctly.