Given a byte, swap the two nibbles in it

preview_player
Показать описание
In this video, will discuss different approaches to swap the two nibbles in a byte. Will also discuss optimized approach, their time and space complexity.

A nibble is a four-bit aggregation, or half an octet. There are two nibbles in a byte. For example, 100 is be represented as 01100100 in a byte (or 8 bits). The two nibbles are (0110) and (0100). If we swap the two nibbles, we get 01000110 which is 70 in decimal.

#QualcommTechnicalInterview
#Swap2Nibbles
#CScode
#Programming
#BitManipulation
#SDEngineer
#CnC++
#Placements
Рекомендации по теме
Комментарии
Автор

No! You're not using a 'byte', as the interviewer requested... you're using an 'int' _(typically 4 bytes)_ Why?

*unsigned char swapNybbles(unsigned char val) { return val <<4 | val >> 4; }*
Paying attention to the details of the question is important! Use correct types! The ANDs are unnecessary!

This is NOT a minor point. Your code doesn't even return the same answer as the code the interviewer asked for! Using 'int' and 'byte' and 'unsigned byte' give different answers. Can you see why?

The 'int' code never changes the outputs sign bit... but it would if you consumed and returned a byte! What if that's critically important? Do you know WHY they're swapping nybbles? No? Then why assume you can force the result to be positive?

For example, what if your code were used as follows...

char byt = 0x0F;
if ( swapNibbles( byt ) < 0 ) { ... // This code will never run, because you used an int //

So, we should probably ask what behaviour is intended here, and whether their byte is signed/unsigned.

And we should probably clarify with the interviewer why/if this should even be a *signed byte* type at all - due to the possible unintended consequences that unexpectedly manipulating the sign bit (MSB) of a *signed byte* might have on surrounding code.

_(read that previous paragraph twice, it's super important!)_

If he does wants signed bytes, then we can easily change our code. We should point out that this would change our 'Logical Shift Right' to an 'Arithmetic Shift Right', which would feed '1's into the high bits as we shift. To get around this, we'd have to mask out the top nybble after performing the Arithmetic Shift Right, to remove any sign-extended '1's that we don't want.

Thus changing...
*unsigned char swapNybbles(unsigned char val) { return val <<4 | val >> 4; }*
to...
*char swapNybbles(char val) { return val <<4 | ((val >> 4) & 0x0F) ; }*

... which is fine, as long as he realises that this operation WILL sometimes turn the result negative - whenever the input values low nybble has its bit-3 set. So, any existing code must be expecting this!

'unsigned bytes/chars' probably make more sense in this situation.


But specifying int's, when you've specifically been asked to handle bytes, is just wrong! It betrays a sense of laziness and lack of attention to detail. And the function signature is not at all what was expected.

We should remember that there's *no such thing as a simple interview question.* Ask clarifying questions... show that you're thinking about any unintended consequences. You have to treat the code like it's a tiny part of a much larger machine that you cannot see.

And, for goodness sake, ALWAYS make sure your 'function signature' matches what they've asked you to produce!

Any monkey can code. They usually want to hire people that also think : )

garychap