Yatra.com Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

Assuming that the number "no" passed to fun endianess need to be changed .

int fun ( int no){
int i = 0,k ; char * c;
c = (char *)(&no); // now c will point to the 1st 8 bits of no.

for( int j =0; j<4;j++){
k = (int)(*c);
i = i | k;
i << 8;
c++
}

return (i)
}

- saikat January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

4 should be sizeof(int) - or sizeof(whatever-type-you-convert).

But if the size of integer is known it is much better (and faster to execute) to write a macro to swap the bytes.

// if sizeof(int) == 4
#define SWITCH_ENDIANNESS(i) ( \
   ((i)<<24) | \
   ((i<<8)&0x0000FF00)) | \
   ((i>>8)&0x00FF0000)) | \
   ((i>>24)&0x000000FF)) )

Alternatively swap the last-byte with first, and the middle-bytes.

// suppose that sizeof(int) == 4
void switch_endianness(int *i) {
    char *c = (char *)i; 
    int temp;
    // first swap 0th and 3rd byte
    temp = *c; *c = *(c+3); *(c+3) = temp;
    // and then swap 1st and 2nd byte
    temp = *(c+1); *(c+1) = *(c+2); *(c+2) = temp;
}

- Selmeczy, Péter January 26, 2012 | Flag


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