Products > Programming

using int32 to store everything from uint8 no int32 with values below 2 billion

(1/3) > >>

So I am working with CAN Open and the faff is that I need to store and send values in 8, 16 and 32 bit signed or unsigned. This makes it impossible to say put them all in an array that would be very useful for say stepping through with easy to write code to send the setup values one after the other.

but it occurs to me. with all the memory I am already using to store pointers to variables that hold the relevant value I am already using 32 bits of memory anyway. And from
what I understand of two's complement representation a negative 8 or 16 bit number stored in a signed 32 bit space looks the same as when stored in an 8 or 16 bit space once the 2 or 3 leading bytes are removed.

Therefore providing where I have an unsigned 32 bit value I do not need to go above the halfway mark of a little over 2 billion then sending the number as a signed or unsigned when I represent an unsigned number in the signed space it makes no difference. This means that I can now have an array of signed 32 bit values that will hold any data type I am using. as I use memcpy() to move the data about the actual storage type is irrelevant. I know what size each variable is anyway as I have to store the information about the data size along with my record of the object dictionary (also in an array), so bingo.

This makes it easier to setup devices when I have to send a sequence of values as I can now while index is has not reached this value ect. In my actual program everything is a signed 32 bit number and the world is a better place for me  :-DD

Umm .. this is more a comment than a question ... but ...

It is a question. To be honest to me it looks like it is made to be so. In canopen land as part of your configuration you have to send the index (16 bits), subindex (8 bits) and number of bits (8 bits) to the devices. so I already have 32 bit values that define the complete "location" of the data and the length. But there is no way to identify signed or unsigned. But if I just store everything as 32 bit signed then the bit length field specifies how much of the 4 bytes to send.

-2 is 0x FF FF FF FE in 32 bit
-2 is 0x         FF FE in 16 bit

so if I need to use this value as 16 bit I can take the 32 bit value and truncate it. Providing I do not have a positive number that is so high it breaks into the negative range a positive signed 32 bit number is the same as an unsigned 32 bit number, 32 bits signed allows me to store 2'147'483'648. The most granular number I have come across so far is mA to define my motor current. I really don't think I will be using a motor that runs on 2'147'483 A (2 MA). Same for voltage in mV. even ┬ÁV.

So am I nuts? or was this all meant to be?

You could probably have a separate array of bytes where you use 3 or 4 bits for every number : 2 bits or 3 bits for size (1 byte, 2 byte, 3 byte , 4 byte, more if you use 3 bits) and one for signed/unsigned

Then store the numbers as a series of bytes. If you want to parse the numbers, you just keep reading 4 bits at a time from the first array, then read up to n bytes from the second array and advance position in both arrays.

If you want to get more fancy, you could use a 32 bit value for every (up to) 8 numbers : 4 bits for the number of values, 4 bits for whatever and 24 bits for the 8 sets of 3 bits (data length and sign) ... or 4 bits for number of values, 28 bits for 7 x 4 bits

if you use uint32 for everything 8 numbers would use 8 x 4 = 32 bytes. with the idea above, 8 uint8 numbers could use 4 bytes + 8 x 1 byte = 12 bytes.

Well you see the problem is that I need to be able to cycle through an array. At the moment I have an array of pointers to the variables. So if I had say at array of single bytes I don't see how I would know the difference between data and the byte/signedness of the data.

So I have to send various variables to devices, at the moment I can cycle through them if I rearrange their pointers in my pointer array, but if I have the pointer array then I may as well use those 4 bytes to store the data, the problem is getting at different data types as though in an array. There is a parallel array that carrys the standard locations like 0x60410010, the index of these is the same as the index for my array of pointers. It means that first I have to setup a variable and assign it a value, then put a pointer t this variable into the pointers array. As all pointers are the same type the array works.

But if I just call everything int32 then the individual variables go away and free up memory and I just use the values at the locations and save pointery stuff.


[0] Message Index

[#] Next page

There was an error while thanking
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod