Cisco Systems Interview Question






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

int x= ( (x & 0xAAAAAAAA)>>1 | (x & 0x55555555)<<1 );

- Anonymous August 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution is 100% correct and working fine....

- Rahul Bhardwaj November 20, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution is correct, short and sweet!

- SN April 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice Man..!!!

- Enigma June 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its the best solution

- Anonymous September 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

its the solution from the CareerCup book

- ucsd May 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

plz explain me how

- Anonymous October 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

many of the above solns seem wrong.. this should work just fine..
x = ((x & 0x33333333) << 2) | ((x & 0xcccccccc) >> 2)

- hgk November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The question was for a byte. It would be more appropriate if we do (same as hgk answer)

N = ((N & 0x33) <<2) | ((N & 0xCC) >> 2)

- MMM April 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

((x<<1)&(10101010)) | ((x>>1)&(01010101))

- karate August 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

((x<<1)&(0x10101010)) |(x&0x01010101)

- gus August 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

opps, should be

((x<<1)&(0b10101010)) |(x&0b01010101)

- gus August 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

logically ok.
karate, by the time the control reaches the second part of the logic x has already been shifted left so right shifting puts it back to its original position with the left most bit missed, possibly. as i know the shifting works in place, so the best option would be to use this way
a = x
left = x<<1 & 0b10101010
right = a>>1 & 0b01010101
x= left | right

- amazonianMonkey August 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@amazonianMonkey
Your logic is not correct, you don't need to shift it back because the send shift is performed on the original x value.

Consider,

int x = 2;
x = x << 1 | x << 1;

if you look at the assembly, assuming %eax has x
add %eax %eax
or %eax %eax

If you print out x, the output is 4 not 12.

- gus August 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define SWAP_BYTE(N)  \
((((1<<0)&(N))<<1)  \
 | (((1<<1)&(N))>>1)  \
 | (((1<<2)&(N))<<1)  \
 | (((1<<3)&(N))>>1)  \
 | (((1<<4)&(N))<<1)  \
 | (((1<<5)&(N))>>1)  \
 | (((1<<6)&(N))<<1)  \
 | (((1<<7)&(N))>>1)  \
)

- Ashutosh August 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is it possible to extend any of these logics to reverse nibble(4 bits) in a number ?

- cunomad August 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes its is possible to swap two nibbles using this concept.

1101 1010 =====> on nibble swap 1010 1101

can be done as follows

x=1101 1010

temp1 = (x << 4) & 0xF0
temp2 = (x >> 4) & 0x0F
result = temp1 | temp2;

- Jayaprakash February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@gus
so you mean to say in case of your solution by the time control reaches the second part (the second part of OR) the value of x is the same as the original, right?
((x<<1)&(0b10101010)) |(x&0b01010101)

i don't think so! How do you explain that...this logic applies to post/pre increment operators in C/C++, but how do you apply the same here, can you write concrete examples? would be nice to make things clear for my understanding. Thank

- amazonianMonkey August 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about XOR ? answer = input XOR ff;

- any August 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

XOR would not work,XORing with ff will just convert all 1s to 0s and vice versa so 11 will be converted to 00, so not correct

- stella August 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sum = 0;m = 2,l = 1
while(p)
{
if(((p&0x03) != 0x03) && ((p & 0x03) != 0x03))
{
p1 = p ^ 0x03;
sum += ((p1 >> 1) &0x01 )*l + (p1 & 0x01)*m;

}
else
{
sum += ((p >> 1) &0x01 )*l + (p & 0x01)*m;

}
m *=4;
l *=4;
p <<= 2;
}
the result is stored in sum

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

a = (num&0x01010101)<<1
b = (num&x010101010)>>1
return a|b

- Dee April 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good 1 !!!

- abc August 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Smart!

- anonymous October 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Howww ?????

- Anonymous November 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What if we XOR with 11110000...?????

- Enigma June 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No... Sorry. I got the question wrong.. :P

- Enigma June 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

v = ((v >> 2) & 0x3333) | ((v & 0x3333) << 2);

- Anonymous June 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

p=((p&m)<<1)|((m<<1)&p)>>1;

this is working code

- karnwal July 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#define ENDSWAP2BITS(x) ((((x)&0x03)<<2)|(((x)&0x0c)>>2)|(((x)&0x30)<<2)|(((x)&0xc0)>>2))

- Anonymous October 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

example:
(0x87)=10000111
After ENDSWAP2BITS:
(0x2d)=00101101

- Anonymous October 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

swapped_no = (((x & 0xC0) >> 2) | ((x & 0x30) << 2) | ((x & 0x0C) >> 2) | ((x & 0x3) << 2));

- Hari November 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int bitswap(char x)
{
int k,l,m,n;
int mask=1;

for(i=0;i<7;i+=2)
{
k=(x & mask)<<1;
mask<<=1;
l=(x&mask)>>1;
mask<<=1;

m=~(1<<i);
n=m & ~(1<<(i+1));
x=(x&n)|k|l;
}
return x;
}

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

#include<stdio.h>
main()
{
short no=158;
int x=0,y=0,z=0;
short temp = 0xFFFF, i=0;

while(no>0)
{
x = (no&1);
y = (no>>1)&1;
temp = 1<<i;
y = y<<i;
temp = (temp)&(y);

z=(z)|(temp);

i++;
temp = 1<<i;
x = x<<i;
temp = temp&x;
z=z|temp;
i++;
no=no>>2;
}

printf("z is %d",z);

}

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

int k = 156; //10 01 11 00
	int t1 = 170;//10 10 10 10
	int t2 = 85; //01 01 01 01

	t1 = t1 & k;
	t1 = t1>>1;

	t2 = t2 & k;
	t2 = t2<<1;

	k = t1 | t2;

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

This should work for all sets of bytes

for (int i = 0; i < no of bytes / 2; i++)
{
u |= (((x>>1)&01 | (x<<1)&02) << (2*i));
x >>= 2;
}

- swarnx February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = (num<<1 & 0xAAAAAAAA);
b = (num>>1 & 0x55555555);
return a|b;

- Dinesh March 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

please tell me how to swap this these numbers.

my num=10110000.
output=10000011.first two bits are moved to 3 and 4 th positions.
please tell me how to do this one.

- arun May 28, 2015 | 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