Open side-bar Menu
 Embedded Software
Colin Walls
Colin Walls
Colin Walls has over thirty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, Colin is an embedded software technologist with Mentor … More »

Questions on data representation

 
December 15th, 2020 by Colin Walls

As I write a lot of articles and publish videos etc., I often receive questions. As a result of a recent piece of Endianness, an embedded software engineer wrote to me. He asked two questions, starting with this code:

unsigned int n = 0x0a0b0c0d;
unsigned char c;

c = (unsigned char) n;

Question

Could you please elaborate a bit on how exactly n, which is 4 bytes, gets copied to c, which is only a single byte wide ?
I dug on the net to find out that we’re truncating some data here. But how exactly that truncation happens in micro steps is not clear.
I mean, does the value of n get copied to a buffer and then truncated and then copied to c or how?

Answer

The exact steps depend somewhat on the compiler and the CPU [instruction set] in question. What is most likely is the n is loaded into a [32-bit] register. Then the least significant byte of this register is stored into c. This process is not sensitive to endianness at all, as the least significant byte is always the least significant byte.

More code for the second question:

union e
{
   unsigned int ui;
   unsigned char a[4];
} f;


f.ui = n;
printf("a[0] = 0x%02x\n", f.a[0]);
printf("a[1] = 0x%02x\n", f.a[1]);
printf("a[2] = 0x%02x\n", f.a[2]);
printf("a[3] = 0x%02x\n", f.a[3]);

Question

I want to know how each memory block gets to be referred as f.a[0] or f.a[1] etc.
What I understand is, the union will have shared memory.
After statement f.ui = n; the data from n will be copied on 4 bytes space of union (which is labelled f.ui) as a 4-byte data chunk (assuming word-size is 4 bytes)
Now, when we print the char array indices, how does the compiler resolve that first byte is f.a[0], f.a[1]

Answer

As you said, f.ui occupies 4 bytes of memory. The array f.a[] occupies the same 4 bytes of memory. So, each bytes of the array f.a[] is assigned one of the [4] bytes of ui. The order of this assignment depends on the endianness of the CPU. On a little-endian device, the output of this code would be:

a[0] = 0x0d
a[1] = 0x0c
a[2] = 0x0b
a[3] = 0x0a

I do hope that this clarifies matters. I do like to receive questions!

Logged in as . Log out »




© 2024 Internet Business Systems, Inc.
670 Aberdeen Way, Milpitas, CA 95035
+1 (408) 882-6554 — Contact Us, or visit our other sites:
TechJobsCafe - Technical Jobs and Resumes EDACafe - Electronic Design Automation GISCafe - Geographical Information Services  MCADCafe - Mechanical Design and Engineering ShareCG - Share Computer Graphic (CG) Animation, 3D Art and 3D Models
  Privacy PolicyAdvertise