Adobe Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
2
of 2 vote

A good explanation here
campuscoke.blogspot.in/2014/12/write-program-to-find-whether-mc-is-big.html

- Jack December 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

unsigned int x=1;
if(*(char*(&x)==1)
print little endian
else
print big endian

- ska February 05, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think this might be a simpler way to find this:

int main()
{
    int x = 1;
    char *cptr
    cptr = (char*)&x;
    if (*cptr)
        printf("Little Endian\n");
    else
        printf("Big Endian\n");
    return 0;
}

- Anonymous March 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

int x=1;
if(*(char*)&x)==1)
 printf("little endian");
else
 printf("big-endian");

- fabregas March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

union testendien{
int num;
char byteval[4];
}check;
main()
{

check.num = 1;
(check.num & check.byteval[3])?printf("little endian"):printf("big endian");

}

- brijesh kumar jaiswal April 11, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include<conio.h>
main()
{
int i,a;
char *p;
i = 0x0102;
p = (char *)&i;
a = *p;
a > *(++p) ? printf("little endian"): printf("big endian");
getch();
}

- Navin Chand April 11, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

"Little Endian" means that the low-order byte of the number is stored in memory at the lowest address, and the high-order byte at the highest address. (The little end comes first.) For example, a 4 byte LongInt
Byte3 Byte2 Byte1 Byte0
will be arranged in memory of a 16 bit machine as follows:
Base Address+0 Byte0 Byte1
Base Address+1 Byte2 Byte3
"Big Endian" means that the high-order byte of the number is stored in memory at the lowest address, and the low-order byte at the highest address. (The big end comes first.) Our LongInt, would then be stored as:
Base Address+0 Byte2 Byte3
Base Address+2 Byte0 Byte1

#include <stdio.h>
main()
{
int x=1234;
int Low_Addr = &x;           //High_Addr = Low_Addr + 1
if((*Low_Addr) == 12)
Printf("Machine is Big Endian \n");
else
Printf("Machine is little Endian \n");
}

- Sunaina May 17, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sunaina gave a good explanation, but the code part has some problems because (*Low_Addr) is always equal to x, i.e. 1234. I copy the solution from wikipedia.org as below.
#include <stdio.h>

int main()
{
union {
short s;
char c[sizeof(short)];
} un;
un.s = 0x0102;
if(sizeof(short) == 2)
{
if(un.c[0] == 1 && un.c[1] == 2)
printf("big-endian\n");
else if(un.c[0] == 2 && un.c[1] == 1)
printf("little-endian\n");
else
printf("unknown\n");
}
else
{
printf("sizeof(short) = %d\n", sizeof(short));
}
return(0);
}

- xxx June 06, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

isn't ska's solution better
compact and simple?

- nuts June 08, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The algorithm proposed by "ska" would not work on a 16 or 64-bit architecture.

If we slightly modify the idea proposed by "ska", we will get the simplest implementation.

unsigned long long x = 0x8877665544332211;

if (*(char*)&x == 0x88)
printf("big endian\n");
else
printf("little endian\n");

This will work on 16, 32 and even 64-bit architectures.
Of course, it will fail on 128-bit architectures....

Justification follows:

unsigned long long x = 0x8877665544332211;

64-bit machine:

Big endian:
--------------------
Base Address + 0: 88
Base Address + 1: 77
Base Address + 2: 66
Base Address + 3: 55
Base Address + 4: 44
Base Address + 5: 33
Base Address + 6: 22
Base Address + 7: 11
--------------------

Little endian:
--------------------
Base Address + 0: 11
Base Address + 1: 22
Base Address + 2: 33
Base Address + 3: 44
Base Address + 0: 55
Base Address + 1: 66
Base Address + 2: 77
Base Address + 3: 88
--------------------

32-bit machine:

Big endian:
--------------------
Base Address + 0: 88
Base Address + 1: 77
Base Address + 2: 66
Base Address + 3: 55
---------------------
Base Address + 4: 44
Base Address + 5: 33
Base Address + 6: 22
Base Address + 7: 11
--------------------

Little endian:
--------------------
Base Address + 0: 55
Base Address + 1: 66
Base Address + 2: 77
Base Address + 3: 88
--------------------
Base Address + 0: 11
Base Address + 1: 22
Base Address + 2: 33
Base Address + 3: 44
--------------------

16-bit machine:

Big endian:
--------------------
Base Address + 0: 88
Base Address + 1: 77
--------------------
Base Address + 2: 66
Base Address + 3: 55
--------------------
Base Address + 4: 44
Base Address + 5: 33
--------------------
Base Address + 6: 22
Base Address + 7: 11
--------------------

Little endian:
--------------------
Base Address + 0: 77
Base Address + 1: 88
--------------------
Base Address + 2: 55
Base Address + 3: 66
--------------------
Base Address + 4: 33
Base Address + 5: 44
--------------------
Base Address + 6: 11
Base Address + 7: 22
--------------------

- rdasilva June 16, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can anyone please explain what
"if (*(char*)&x == 0x88)" means? We are taking the address of x and what exactly does the *(char*) do? Also, why are we comparing left hand side expression with 0x88?

- Anonymous December 24, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

*(char*) -> converting the pointer of the int to a char pointer, then de-referencing it. On a 32 bit system, this wud return the 1st byte used by x.

- AK January 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why would Ska's Code not work on 16 and 32 bit m/c...could u please explain??? According to my understanding it should work. correct me with example if i m wrong.

- Anonymous March 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops...I meant 16 and 64 bit as u said.

- Anshul March 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#define MAX sizeof(int)
typedef union {
int val;
char str[MAX];
}testend;

main()
{
testend tend;
tend.val = 1;
if(tend.str[0] == 1)
printf("little endian");
else
printf("Big endian\n");

}

- brijesh kumar jaiswal December 27, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include<iostream.h>
#define max sizeof(int)
typedef union {
int val:
char str[max];
}
testend
void main()
}
testend tend;
tend.val=1;
if(tend.str[0]==1)
cout<<"little endian:";
else
cout<<"big endian\n:";
}

- kundan kumar March 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hey just to answer ur question
(*(char*)&x == 0x88)" means--we r typecasting the int value to char which is just one byte long and which would only take the lowest memory address from the int value . if this value is the highest value part ie 0x88 then we conclude it is big endian else little endian

- naja January 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Have a look at this code.....

int i =1;
(i>>=1) ? puts("Big Endian") :puts("Little Endian");

- Sharath Bhaskar March 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no it won't work. Think

- ND April 16, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Int x=1;

      if(x&=01)
      cout<<"LITTLE ENDIAN";
      else cout<<"BIG ENDIAN";

how abt this?

- Anonymous May 31, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

no its not gonno work..

- vicky June 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Almost all the correct approaches have been posted. Just want to post a slightly different one having the use of bitfields. Please confirm if anyone sees it as incorrect.

int x = <no>;
struct bitfield
{
int mybit : 1;
} bf;
bf.mybit = x;
(bf.mybit & 1) ? printf ("little endian") : printf ("big endian");

- Anonymous November 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Almost all the correct approaches have been posted. Just want to post a slightly different one having the use of bitfields. Please confirm if anyone sees it as incorrect.

int x = <no>;
struct bitfield
{
int mybit : 1;
} bf;
bf.mybit = x;
(bf.mybit & 1) ? printf ("little endian") : printf ("big endian");

- hary November 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess i missed something <no> should actually be a value 1.

- hary November 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

java code:
java.nio already has this support

use BytOrder class

* Retrieves the native byte order of the underlying platform.

Byteorder.nativeOrder() (now simply check if it Byteorder.BIG_ENDIAN or not)

- Arvind February 03, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More