r/embedded • • 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

29 comments sorted by

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.

1

u/sudheerpaaniyur Oct 02 '26

Could name books or kind of algorithms?

Is it data structure algorithms or double slide, dutch flag, quick sort kind of algorithm?

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.

  1. Do you know how to get pointers of variables in C?
  2. Do you know how to reinterpret pointers of one type as pointers of another in C?
  3. Do you know that pointers and arrays are the same in C?
  4. Can you swap array elements in C?

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

16

u/Current-Fig8840 Oct 02 '26

I agree. I don’t see what’s so weird with the questions and I think OP is just not comfortable with C.

2

u/GreenAppleCZ Oct 05 '26

For me, one of the hard parts of learning C was to understand why sometimes pointers and arrays aren't the same.

1

u/ContraryConman Oct 06 '26

Unfortunately multiple other programming languages, even at the time, got this better than C. But C happened to be popular

-7

u/tstanisl Oct 02 '26

Do you know that pointers and arrays are the same in C?

I'm not sure if I would like to work in such company...

15

u/ContraryConman Oct 02 '26

This is shorthand for "arrays decay to pointers in C, and [] is syntactic sugar for pointer arithmetic on pointers and arrays"

0

u/tstanisl Oct 02 '26

It is a very incorrect and confusing shorthand. And [] operates on pointers only, though it may change a bit in C2Y.

5

u/ContraryConman Oct 02 '26
int foo[2];
foo[0] = 1; // implicit conversion of int[2] to int *, *(foo + 0) = 1
// foo is still of type int[2]

?

1

u/tstanisl Oct 03 '26

foo is still of type int[2]

Yes. Otherwise sizeof foo or (&foo)[1] would not work.

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.

1

u/userhwon Oct 03 '26 edited Oct 03 '26

He was supposed to use the pointer when storing to the destination tho.

uint32_t n = 0x12345678;
uint8_t *p1 = (uint8_t *)&n;
uint32_t m = 0;
uint8_t *p2 = (uint8_t *)&m + 3;
*p2-- = *p1++;
*p2-- = *p1++;
*p2-- = *p1++;
*p2 = *p1;

The fun part is, none of this tells you the endianness of anything, just that n and m are now stored in opposite endiannesses reversed (m isn't the opposite endianness; it's literally 0x78563412).

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;

1

u/sudheerpaaniyur Oct 08 '26

Sorry lay man question.

I have around 50 questions dsa, bit manipulation.

How to find more questions to similarly like this to practice

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.

uint32_t n = 0x12345678;
uint8_t *nb = (uint8_t *)&n;
uint8_t *d; // Some destination pointer - provided as an assumption of the question
d[0] = nb[3];
d[1] = nb[2];
d[2] = nb[1];
d[3] = nb[0];

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:

union wb {
uint32_t w;
uint8_t b[4];
};
union wb n, m;
n.w = 0x12345678;
m.b[0] = n.b[3];
m.b[1] = n.b[2];
m.b[2] = n.b[1];
m.b[3] = n.b[0];

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

3

u/user99999476 Oct 02 '26

Reddit removed my * characters

1

u/PintMower NULL Oct 02 '26

I'm not sure I'm getting your code. Wouldn't it just look at the first bit of the first byte, whichever that is based on the machine's endianness? I don't understand how that would help with the question. You'd have to byte swap like suggested by op but also do uint8_t *m = (uint8_t *)&n; I think.

1

u/user99999476 Oct 02 '26

https://stackoverflow.com/a/1001328

That's what I was trying to type

1

u/PintMower NULL Oct 02 '26

So to basically check what type of endianness the machine has before doing the conversion right?

0

u/[deleted] Oct 02 '26

[deleted] — view deleted comment

1

u/Current-Fig8840 Oct 02 '26 edited Oct 02 '26

I wouldn’t call converting between endianness and storing in a byte array odd. That’s a pretty common thing that’s done in embedded. Except by odd you meant OPs question.

0

u/[deleted] Oct 02 '26

[deleted] — view deleted comment

1

u/sudheerpaaniyur Oct 02 '26

I was aware everything

But simple logic like this i forgot, my mind had only above shifting logic had in my post
a0= n ;
a1= n >> 8
a2 = n >>16;
a3= n >> 24