News:

;) This forum is the property of Proton software developers

Main Menu

Include negative numbers in an SWORD Array

Started by Dave-S, Mar 06, 2022, 06:31 PM

Previous topic - Next topic

Dave-S

I have a SWORD Array and SWORD variables, when the values are negative it does not put the negative number in the array.  A -1 is put as 65535 and a -10 is 65526.
How do I get to put -1 and -10 in the array?
Thanks David

trastikata

David, the compiler places the negative numbers, but you have to display them with the SDec modifier.

Dave-S

Cannot use SDEC to load the array compiler will not accept. I am loading the array into a memoary chip and the numbers are 65535 and are therefore detected as negative.
David

trastikata

Quote from: Dave-S on Mar 06, 2022, 07:27 PMCannot use SDEC to load the array compiler will not accept. I am loading the array into a memoary chip and the numbers are 65535 and are therefore detected as negative.

David, I am not sure I understand what you are trying to achieve. -1 in two's complement is 1111111111111111 which is 65535 and -10 in two's complement is 1111111111110110 which is 65526 - thus everything works and it's a question of representing (Printing) the variables. Signed variables should use the SDec modifier when being displayed. 

Dave-S

I have a file with SUN tracker positions which I have loaded into the Array and have copied them to the memory chip, when the Tracker extracts the information from the memory chip to calculate the difference between that and the tracker position given by the Accelometer, so as to move it to the correct postion, it is comparing 65536 with values between -10 to 30. So does not work.  The calculations are being done with SWORD variables. The Accelometer negative values are read via a SWORD variable.

David

trastikata

Quote from: Dave-S on Mar 06, 2022, 08:43 PMit is comparing 65536 with values between -10 to 30. So does not work.  The calculations are being done with SWORD variables. The Accelometer negative values are read via a SWORD variable.

David, I think there is a fundamental misunderstanding here. Please read about the different number representations in programming:

-10, 65526, FFF6

Those three numbers are written in memory/MCU as binary code 1111111111110110 - how that binary code will be interpreted depends on the variable type where that binary code (1111111111110110) is being stored into.

The values are being correctly stored into the memory, their interpretation by your program is however incorrect. Please check your variable assignments!

top204

#6
Trastikata is correct. The compiler uses 2's complement to hold negative values, which tends to be the standard mechanism in computing.

I was going to give the link to Wikipedia for 2's complement, but as is usually the case with it, its explanation is so complex and convoluted, it does not actually teach, it just confuses a simple process. :-)

The simplest way of explaining 2's complement is that the MSB (Most Significant Bit) of a value holds the sign, and is set for a negative value and clear for a positive value, and if negative, all the bits after a value are set with it. To give an example... -1, in 8-bit mode, is calculated by "(255 - Value) + 1", which leaves the value 255. i.e. $11111111. So -2 will be the value 254 (%11111110). i.e. "(255 - 2) + 1" That calculation comes into play if the value's MSB (Most Significant Bit) is set. If it is clear, the positive value is as it is. :-)

The same for 16-bit mode, where the calculation is "(65535 - Value) + 1", and so on...

That means for an 8-bit variable, it can hold -128 to 127, and for a 16-bit variable it can hold -32768 to 32767, and for a 32-bit variable, it can hold -2147483648 to 2147483647. Notice that the negative maximum value is always 1 greater than the positive value? That's because the MSB is always set on them.

Yasin

Growing up like a digital electronic baby. So from the very beginning. I wish 1 and 0 were not taken as a basis. If it consisted of -1,0,1, such problems would not exist.  ;)