Cisco Systems Interview Question Software Engineer / Developers

  • cisco-systems-interview-questions
    0
    of 0 votes
    17
    Answers

    Write a function which sets the bits in an integer when start bit position and end bit positions are passed as argument.

    For eg, if I call function setbit(2,4) , it should return 14. (1110)
    least significant bit on 1st position and so on..

    - lippie on March 21, 2012 in United States for Switching Softwares Report Duplicate | Flag
    Cisco Systems Software Engineer / Developer Algorithm C

Team: Switching Softwares
Country: United States
Interview Type: Phone Interview


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

Set the range of bits from #L (least) to #H(highest)
1. Set the bit #(H-L+1)
2. Subtract 1 from the result
3. Shift the result by L bits to right
Just omitting the corner case checks to make the code clean.

#include <stdio.h>
int set_bits_range(int start, int end)
{
    return ( (1 << (end - start + 1)) - 1 ) << start;
}
int main (int argc, char *argv[])
{
    int rv = 0;
    rv = set_bits_range(3, 5);
    printf("%d\n", rv);
    return 0;
}

- ashot madatyan on May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Since index is starting from 1.

int set_bits_range(int start, int end)
{
    return ( (1 << (end - start + 1)) - 1 ) << (start-1);
}

- Psycho on May 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

What about the below code, please give feedback

int setbits(int start, int end) {
int no = 0;
int i;

for(i = start -1 ; i <= end - 1; i++) {
no |= (1 << i);
}

return no;

}

- Mohamed Mustafa on March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

works

- coolpyro on April 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

number |= (((1 << end+1) - 1) ^ ((1 << start)-1));

- Abhinav on April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Previous answer is when LSB starting at 0
For LSB as 1:

number |= (((1 << end) - 1) ^ ((1 << start-1)-1));

- Abhinav on April 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<math.h>
int createint(int startbit, int endbit)
{
int finalint = 0;
int i,orint;
for(i=startint; i <= endbit; i++)
{
orint = (int)pow(2,i);
finalint = finalint | orint;
}
return finalint;
}

- candidate on March 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

unsigned int SetBits(unsigned int Num, unsigned int From, unsigned int To)
{
return (Num | (((unsigned int)~0 >> (32 - (To - From + 1))) << From));
}

- Guy on March 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

int a[4];

void createint(int startbit, int endbit)
{
for(int i = startbit; i <= endbit; i++)
a[4-i] = 1;
}

int main()
{
for(int i = 0; i < 4; i++)
a[i] = 0;
createint(2,4);
for(int i = 0; i < 4; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}

- Candida on March 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2^(end-start )<<start

- Anonymous on April 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

2^(end-start )-1<<start

- Anonymous on April 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
if start=2 and end =6 ,index starting from 1 then result should be 111110 {{{ { int num=0,start=2,end=6,i,result=0; for(i=start-1;i<end;i++) // if you start index from 0 then i=start num+=pow(2,i); result=result|num; printf("%d",result); return 0; } - mangal.govind on April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
if start=2 and end =6 ,index starting from 1 then result should be 111110 {{{ { int num=0,start=2,end=6,i,result=0; for(i=start-1;i<end;i++) // if you start index from 0 then i=start num+=pow(2,i); result=result|num; printf("%d",result); return 0; } - mangal.govind on April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if start=2 and end =6 ,index starting from 1 then result should be 111110

{
 int num=0,start=2,end=6,i,result=0;

for(i=start-1;i<end;i++) // if you start index from 0 then i=start 
 num+=pow(2,i);

result=result|num;
printf("%d",result);
return 0; 
}

- mangal.govind on April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if start=2 and end =6 ,index starting from 1 then result should be 111110

{
 int num=0,start=2,end=6,i,result=0;

for(i=start-1;i<end;i++) // if you start index from 0 then i=start 
 num+=pow(2,i);

result=result|num;
printf("%d",result);
return 0; 
}

- mangal.govind on April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int setBit(int start, int end)
{
int val = 1, val1;

end -= start;

while (start--) val <<=1;

while (end--) {
val1 = val;
val <<=1;
val = val | val1;
}

return val;
}

- Genisim on April 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int setbits(int start, int end) {
int no ;
int i;
int y = pow(2,end+1-start);
y = y-1;
printf("%d \n",y);
no = y<<(start-1);
return no;

}

- softy on June 06, 2012 | Flag Reply


Add a Comment
Name:

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

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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