0

Background: I am sampling a signal from accelerometer at a rate of 20KHz( period of every 50 micro seconds). I need to store this information in memory card. Kit: STM32F40, IDE: Keil. ADC Input is 8 bit. I am trying to store numbers in an array .

Question: How a variable is stored in processor? As per keil/ other sources it depends on compiler. Compiler would take atleast 4 bytes of memory to save an integer. for example if a number is 1024 how is it stored in processor ? will it be stored as 1000000000 or the individual number i.e., 1,0,2,4 gets stored individually ? if the number is 256 how is it stored in processor ?

  • It depends heavily on the processor in question. Most sixteen-bit processors would store a value of 256 as either 0x0100 or 0x0001, depending on endianness. A pure 8-bit processor wouldn't be able to store 256 natively, and you'd need to do software handling for larger numbers than 255. – Hearth Nov 27 '18 at 19:44
  • OP is using STM32F40 which is ARM Cortex-M4 32 bit RISC – JBaczuk Nov 27 '18 at 20:05
  • There's no such thing as an STM32F40. There are various STM32F4xx and STM32F40x however. – Chris Stratton Nov 28 '18 at 02:15
  • Additionally an "integer" is a generic term, while an int has specific meaning in the context of a C compiler; pretty much any for any of the STM32 series would have a 32-bit or 4-byte little endian int. But the question is unanswerable because we can't see what data type your program is using, if you have even written one yet. – Chris Stratton Nov 28 '18 at 02:16
  • @ChrisStratton Thanks for the answer. I understood, I have used uint16_t adc_value – user3264653 Nov 28 '18 at 15:31

1 Answers1

3

Most modern processors store integers using binary representation - with a byte being 8-bits. So a 4-byte number could store 32-bit giving a range of numbers:

  • Unsigned numbers in the range 0 to 4294967295 (\$2^{32}\$)
  • Signed numbers in the range -2147483648 (\$-2^{31}\$) to 2147483647 (\$2^{31}-1\$). These are typically stored using two's complement.

The exact size of the integer depends on the type you use. For C compilers, these are typically (unsigned) char as 8-bit, (unsigned) short as 16-bit, and then (unsigned) int is typically 32-bit. There is also an (unsigned) long which can be either 32-bit or 64-bit depending on compiler settings and processor.

For variables with a size that is consistent across platforms, you can use the types in <cstdint> - these are (*): (u)int8_t, (u)int16_t, and (u)int32_t, with optionally (u)int64_t and (u)int24_t.

As you have said you wish to store a 4-byte (32-bit) integer. The exact way this is stored in memory depends on the processor - there are two flavours, called the endian-ness of the processor:

  • Big-Endian - stores the most significant byte (upper bits) in the lowest memory address. The rest of the bytes are stored in sequentially reducing order of significance.
  • Little-Endian - stores the least significant byte (lower bits) the lowest memory address. The rest of the bytes are stored in sequentially increasing order of significance. The STM32F40 falls into this category.

For completeness, fractional numbers are typically stored using IEEE 754 floating point representation is used, with single precision (~7 d.p.) being 32-bit, and double precision (~15 d.p.) taking 64-bit.


(*) (u) determines if signed or unsigned - e.g.uint8_t is unsigned 8-bit, int8_t is signed 8-bit.

Tom Carpenter
  • 65,995
  • 3
  • 145
  • 204