## Cisco Systems Interview Question Software Engineer / Developers

• 0

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..

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;
}``````

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);
}``````

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

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

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

return no;

}

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

works

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

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

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

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

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

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;
}

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));
}

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;
}

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

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

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

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

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; }
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; }
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;
}``````

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;
}``````

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;
}

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;

}

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.

### 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.

### 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.

### 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.