Embedded Software 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 representationDecember 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; QuestionCould you please elaborate a bit on how exactly n, which is 4 bytes, gets copied to c, which is only a single byte wide ? AnswerThe 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]); QuestionI want to know how each memory block gets to be referred as f.a[0] or f.a[1] etc. AnswerAs 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! |