Microsoft Interview Question
Country: United States
As it is not string literal but an integer literal
i tried to split the elements in the array and put arr[]={'0','x','3','b','2','4'} and swap the elements arr[2] with arr[4] and arr[3] with arr[5].
The code was to be written in C. I could have used perl/python split and join but I am unaware of any such method for integers in C.
Also the output has to be integer so putting the elements separately in arr[ ] doesnt seem to me a good idea. Anybody having any different approach?
I think its basically to Swap Nibbles..In the firstcase find the mask That is Number of Bytes / Nibbles needs to be Swapped...Count the number of Bits /Bytes given as input make.The mask will be 00FF for 2 bytes for (4 hexa Characters) And 000FFF for 3bytes for (6 Hexa characters.)
- Lucy October 26, 2012Then Just perform the operations
First case - k1 = 243b & 0 0 FF
k2 = 243B >>(No of bytes / 2 )*8in the input
Then k = k1 | k2 is the answer
Note - Our Mask will also be Number of Bytes / 2 ---- Numbers of F's
In first case input is of 2 bytes so Mask is of 1 byte = 00FF
In 2nd case input is of 3 bytes so mask is of 1.5 = 000FFF