Qualcomm Interview Question
Developer Program EngineersCountry: United States
Interview Type: In-Person
I would use something much simplier :
const long number=123;
if(number == htonl(number)){
printf("This is big endian machine\n");
} else {
printf("This is little endian machine\n");
}
Let's say a=0xAB FE CD FF
1002 | 1003 | 1004 | 1005
----------------------------------------------------------------------------
Big Endian FF CD FE AB
Lil Endian AB FE CD FF
-----------------------------------------------------------------------------
seems to be not correct, for lil endian as LSB has to be at lowest address:
correct representation is the reverse:
1002 | 1003 | 1004 | 1005
Lil Endian FF CD FE AB
Big Endian AB FE CD FF
int main(){
unsigned int i;
char *c = (char *)&i;
if(*c){
printf("Littele indian\n");}
else{
printf("Big indian\n");}
}
@flipper: Please stop surround your ENTIRE comment with squiggly braces. Surround ONLY the source code. If you are not writing source code, do not use squiggly braces. Look at how everyone else is doing it.
What should be the output if I changed it to
int main(){
unsigned int i=0;
char *c = (char *)&i;
printf("%d",*c);
if(*c){
printf("Little indian\n");
}
else{
printf("Big indian\n");
}
getch();
}
@mkimar9009
its going to say Big endian; but obvious it's wrong.
You won't be able to differentiate between Little endian zero & a big endian zero.
Need to seed the test data properly.
can have a check like this
if(i) { // we are using the integer data type here to make sure that the test data is valid.
// Do the test of endiness here.
}
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv) {
int foo = 1 + 2*256 + 3*(256*256) + 4*(256*256*256);
int i;
char *p = &foo;
for (i = 0; i < 4; ++i) {
printf("%d: %c\n", i, '0' + p[i]);
}
return 0;
}
Lil endia-> MSB to LSB are kept at high to lower memory address
big endian-> MSB to LSB are kept at lower to higher address location
Let's say a=0xAB FE CD FF
1002 | 1003 | 1004 | 1005
----------------------------------------------------------------------------
Big Endian FF CD FE AB
Lil Endian AB FE CD FF
-----------------------------------------------------------------------------
Endian means the byte ordering;
$ echo -n I | od -to2 | head -n1 | cut -f2 -d" " | cut -c6 is the command to knw endianness of system
If it gives 0 then big if 1 then lil
}
- karthiga.m1988 March 06, 2013little endian machine, gives “67 45 23 01″ as output , big endian machine, gives “01 23 45 67″ as output.