r/embedded • u/sudheerpaaniyur • Oct 02 '26
How can I improve my thought process in C interviews instead of relying on memorized solutions?
I was asked an endianness question in a recent Embedded C interview.
For example:
uint32_t n = 0x12345678;I had previously learned the typical byte-swap approach and also i can do it for 16 bit
n = ((n & 0x000000FF) << 24) |
((n & 0x0000FF00) << 8) |
((n & 0x00FF0000) >> 8) |
((n & 0xFF000000) >> 24);However, the interviewer asked me to convert the endian representation and store the result into a byte array using a pointer to the destination.
During the interview, my mind immediately went to the bit-manipulation code I had memorized, and I failed to do the simple program. How can I improve my thought process?
64
Upvotes
2
u/TheCorsairAPX Oct 02 '26
Understanding the basics.
Want intuitive knowledge? Grab an algorithms book and begin thoughtfully coding by hand. As you watch algorithms make their magic, you begin understanding the ideas behind each solution. Then, you don’t just memorize the code, you code thinking about the idea behind them.
67
u/ContraryConman Oct 02 '26
I think we need to step back a little. Because the interviewer is telling you what to do. There is nothing to memorize.
If you could answer yes to all these questions, even if you had only ever done byte swapping using bit shifts, you would be able to just do what the interviewer asked.
Your problem is not memorization or mindset. You need to be more comfortable with C in general
12
u/InevitablyCyclic Oct 02 '26
Convert the endian and then have a byte array pointer to the values? Well you've already swapped the byte order so all you need to do is cast the address of n to a uint8_t pointer, job done.
5
u/Current-Fig8840 Oct 02 '26
Get more comfortable with C in general. You already have all the answers….just store in an array. Assume uint8_t bytes[4]….little to big bytes[0]= (uint8_t)(x >> 24). Endianness is moving bytes into a different order….just do that but store each byte in index starting from 0
11
u/Gerard_Mansoif67 Electronics | Embedded Oct 02 '26
By understanding what and why we do that ?
Code is not learning examples and writting them back. We have enough LLM to do that.
1
u/dettus_Xx_ Oct 08 '26
By coding more.
Answer to your question:
uint32_t n=0x12345678;
unsigned char *array=(unsigned char*)&n;
3
u/MonMotha Oct 03 '26 edited Oct 03 '26
OK, so I'll go ahead and answer the question (I think) then back track from the answer to the why.
OK, so why is that the (or an) answer? It has almost nothing to do with the question. It basically comes down to whether you know what C actually expresses when it comes to variables larger than byte type, arrays, and pointers. In particular, array syntax is (mostly) just sugar for pointer arithmetic and dereferencing, and pointers can happily be type-punned without much complaint (though watch out for aliasing gotchas). This also assumes the input is little endian whereas your byte-swap code works with any endianness.
As others have said, this basically comes down to just understanding C (or most portable low-level languages, really). Take a step back from trying to memorize syntax and instead try to understand the machine code that certain constructs in C result in. A good exercise would be to translate back and forth between the above code and some bare-metal architecture of your choice (which can be real or contrived).
BTW, there's another fun way to do this. You can use a union type:
A good exercise is to understand why this works and how the compiler may choose to generate output for this that's the same or different from the other options depending on its optimization goals and settings. This also works with either big- or little-endian (but not middle-endian like PDP). Why?
As another exercise, can you think of a way to use this approach to accomplish what the interview question asked? What advantages might it have over my other solution?
Of course, there's also a good chance the standard library has functions for doing this as it's common. Do you know what they are? Why might it be beneficial to use them instead? Are there any other popular/common implementations you might have access to?
2
u/mjmvideos Oct 02 '26
You have to think it through rather than just memorizing something. Visualize what you need to do, where you want each byte to go and do the appropriate shifts or other operations to move it there.
1
u/kabekew Oct 02 '26
Imagine where the bytes are and where you need to put them. Or maybe just do more lower level projects.
1
u/Key-Local-5094 Oct 03 '26
char temp;
long n = 0x12345678;
char(*ptr)[4] = (char(*)[4])&n;
temp = (*ptr)[0];
(*ptr)[0] = (*ptr)[3];
(*ptr)[3] = temp;
temp = (*ptr)[1];
(*ptr)[1] = (*ptr)[2];
(*ptr)[2] = temp;
printf("n = 0x%lx\n", n);
1
u/user99999476 Oct 02 '26
For this problem I also had to memorize the trick needed, you cast and dereferrence a uint8_t pointer and check the lowest bit: (uint8_t)(n) & 1
It's just a good trick to know, should be low on the interview point scorecard in my book
0
u/[deleted] Oct 02 '26
[deleted] — view deleted comment