Amazon Interview QuestionSDE1s
Country: India
Interview Type: In-Person
If I understand it right, big and little endian should only affect the way they are stored in memory address and not their binary code representations.
otherwise a program containing a bitwise shift operator would not be portable since the meaning would change e.g. if lets say a represents 13 then your intention in a >> 1 would be to push bits in a to right by 1 position and you would expect 6 as the result. if a is binary 00000000, 00000000, 00000000, 00001101 then you would get what you expect instead if a is coded as 00001101, 00000000, 00000000, 00000000 then a >> 1 would give a different number altogether making the operation non-portable.
What I am writing here is how the number is stored in memory. The compiler can do that conversion for us automatically, so we don't need to care about if the machine is using big endian or small endian when we are programming. For example, if you use GDB to debug c or c++ programs, gdb will do that conversion for you.
ok if compiler or some other layer beneath our program abstracts the big-endian vs little endian format for the program thus ensuring that the program works consistently across different systems then this program need not try to change the input to big-endian. Isnt it?
I think it just aims to check if we know how a number is stored in big endian and little endian machine or system. As you know that almost all of our personal computers are using little endian and the internet protocol is using big endian. So the network adaptor has to do this conversion for us. Anyway, I just treat it as a chance to review endianness.
Actual intention of this problem is how the machine represents big endian and little endian.
The byte orders we have to concentrate as mentioned in the first comment.
Based on input parameter format( big or little endian ) value, we should print the binary numbers.
example: input 13 , big endian
big endian : 00001101 00000000 00000000 00000000
private static byte[] reverseArray(byte[] b)
{
byte[] newArr = new byte[b.length];
for (int i = 0; i < b.length; i++)
{
newArr[newArr.length - i - 1] = b[i];
}
return newArr;
}
public static StringBuffer getBinary(int num, boolean isBigEradian)
{
byte[] bytes = new byte[4]; //assuming 32 but int
int index = -1;
while (num != 0)
{
bytes[++index] = (byte) (num & 255);
num = num >> 8;
}
if (isBigEradian)
bytes = reverseArray(bytes);
StringBuffer sb = new StringBuffer();
for (byte b : bytes)
if (b != 0)
sb.append(convertByteToString(b));
return sb;
}
private static StringBuffer convertByteToString(byte b)
{
StringBuffer sb = new StringBuffer();
int count = 0;
while (b != 0)
{
sb.insert(0, b & 1);
b = (byte) (b >> 1);
count++;
}
while (count < 8)
{
sb.insert(0, "0");
count++;
}
return sb;
}
public static void main(String args[])
{
System.out.println(getBinary(786, false));
}
public class DecimalToBinaryConverter {
public String decimalToBinary(int num, boolean bigEndian) {
StringBuilder strBr = new StringBuilder();
while (num > 0) {
strBr = bigEndian ? strBr.insert(0, num % 2) : strBr.append(num % 2);
num = num / 2;
}
return strBr.toString();
}
public static void main(String[] args){
DecimalToBinaryConverter convrtr = new DecimalToBinaryConverter();
System.out.println(convrtr.decimalToBinary(11, true));
System.out.println(convrtr.decimalToBinary(11, false));
}
}
results are
1011
1101
You answer is wrong because you miss understood the concept of little endian and big endian. Let's take 13 as an example, its binary format is:
00000000, 00000000, 00000000, 00001101, this is also the representation of little endian. The big endian format is 00001101, 00000000, 00000000, 00000000. You have to reverse the bytes, not the bits. That's where you got the answer wrong. Please refer to my code for the corrent answer.
The assumption that I am making here is that the output has to be presented in a big or little endian formatted binary number and not that the input is big/little endian formatted.
If I understand it right, big and little endian should only affect the way they are stored in memory address and not their binary code representations. Otherwise I guess programs containing shiftwise operators would not be portable.
The logic in my above code is still flawed though since as you pointed out the format is not applicable at bits instead it is at bytes.
Let's take 13 as an example, its binary format is:
00000000, 00000000, 00000000, 00001101, this is also the representation of little endian. The big endian format is 00001101, 00000000, 00000000, 00000000. You have to reverse the bytes, not the bits.
- ravio June 07, 2014