Ittiam Systems Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

If 'x' in an int input and 'm' & 'n' are indices with 0-base as LSB, then the output can be written in one instruction as
y = (x & ((1 << m) - (1 << (n+1)))) >> (n+1);

Explanation:
1) (1<<m) returns 2 to the power m
2) (1<<(n+1)) returns 2 to the power (n+1)
3) Difference of above 2 returns the mask with all bits set between m and n (both exclusive)
4) Now AND the above mask with original input 'x' to extract bit values for only the bits between m and n (both excluded)
5) I assumed that the expected result needs to be returned considering the (n+1)th bit as LSB, and hence the last right shift operation for (n+1) times.

Let's take an example:
int m = 6; //1st index
int n = 1; // 2nd index
int x = 173; //input = 10101101

int y = (x & ((1 << m) - (1 << (n+1)))) >> (n+1);

//(1<<6) - (1<<(1+1)) = 01000000 - 00000100 = 00111100
//x & 00111100 = 10101101 & 00111100 = 00101100 (144 in base 10)
//finally, y = 00101100 >> (1+1)= 00001011 (11 in base 10)

- ravi December 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

(x&(~(~0<<m))) >> (n-1)

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

((unsigned char)(c << (n - 1))) >> (8 - (m - n + 1))

- nim September 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

((i << (8-m-1)) & 0xff) >> (n+8-m-1)

- colriot September 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if indices start from 1

ans = ((i & (-1 << (8 - n)) >> (8 - n)) & ((1 << (n - m + 1)) - 1)

- Anonymous September 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Define "single instruction". If it means 1 assembly instruction, that's only possible if value's already in a register and either we know m and n at compile time so we can prepare the appropriate bitmask, or if a special "bit range extraction" instruction exists.

- eugene.yarovoi October 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

single instruction in the sense you need to write the logic in a single C statement.

- Sunil bn October 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

if i is the byte given, we can extract bits between n and m where m>=n with the following single instruction.

[i & [(0xFF << m) ^ (0xFF<< n)]] >> n

how this works:
we create 2 maska (Assuming m=5 and n=2)

11100000 0xFF << m
11111100 0xFF << n

Now we take xor of the above which gives us below mask

00011100 [(0xFF << m) ^ (0xFF<< n)]

Now if the apply the & gate with the given byte i (Assuming its 01010101) it gives us below byte.

00010100 [i & [(0xFF << m) ^ (0xFF<< n)]]

Now we can shift the byte n times to the right to get the extracted byte

00000101

-------------------------------------------

Please let me know if you see any improvement or if you have any comment.

- asamra1986 October 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We need to construct a mask having all the bits between m and n set to 1.

which is = 2^m + 2^m-1 + 2^m-2 .... + 2^n
= 2^n (1 + 2 + 2^2 ..... 2^m-n)
= 2^n(2^(m-n+1)-1)
= 2^m+1 - 2^n
That's how you'd get the mask in a single instruction.

- godlIkeNoob December 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void function(int i, int j, int orig_number){
int mask = 0xFFFFFFFF;
return ((((mask<<j)&&(~(mask<<i)))&&orig_number)>>j);
}

- code_monkey December 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Apologies for a small mistake in the above code.
The '&&' should be '&'.

- code_monkey December 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char y = ((x << 8 - m) >> (8 - m + n));

- chandan.jc March 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

indices start from 1

((x >> (n-1)) & (0xff >>> ((8-m) + (n-1))))

- chaitea May 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(x&( ((1<<m) -1) & ~ ( (1<<(n+1)) -1)) >> (n+1)

- Meenu July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

(x>>m-1)&((1<<m-n+1)-1)

- Aashish July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

main problem is of extraction and u have not given extraction.
you have given answer in last bainary indice.

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

bits=((~(0xFFFF<<(n-m+1))<<m)&x)>>m;

where
x is the given number

- pras July 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()
{
int a = 214;
int m=6,n=3;
int c;
c = a & ((1<<(m+1)) - (1<<(n)));
c = c >> n;
printf("%0x",c);
return 0;
}

- johns November 30, 2014 | 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