## Texas Instruments Interview Question for Software Engineer / Developers

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

for(count=0;count<31;count++) // 31 if it is a 32 bit integer
{
if((N>>count)&1==1)
{
print(the position is count)
break;
}
}

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

gud method

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

superb method.. this is what u can think on top of your head in an interview !!!

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

Here is recursive program which is easiest way

``````int func(int);
void main()
{
int no=104;
printf("%d",func(no));
}
int func(int no)
{
if(no%2==1)
return 1;
return func(no/2)+1;
}``````

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

If N is the number, N&-N will give you the first bit set

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

Answer is incomplete. This will not return the position. The bit needs to be shifted in order to count the position.

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

N &(~N +1) will give you the correct position

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

``````N=N &(~N +1)
for(m=n;m>0;m=m/2){
cont++;
}
printf("%d\n",cont);``````

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

#include<stdio.h>
#include <math.h>
main()
{
float n = 4.0;
printf("\nThe bit position is at: %f\n", sqrt(n)+1);
}

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

Here is another solution: geeksforgeeks org/ ?p=555
log2(n&-n)+1

However it is costly because of using log2.

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

little optimized

``````int bitset(unsigned long num)
{
int count = 0;
while(num) {
switch (num & 0xf) {
case 1:
case 3:
case 5:
case 7:
return count + 1;
case 2:
return count + 2;
case 4:
return count + 3;
case 8:
return count + 4;

default:
break;
}
num >>= 4;
count += 4;
}
}``````

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

the case 8: never happens, this was code snippet I copied from much more optimized code.

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

@Prem Mallappa The code doesn't work if the num is in between 9 to 15....

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

int getPos(int x)
{
int count = 0, y = 0;
while(!y)
{
y = x & 01;
x = x >> 1;
count++;
}
return count;
}

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

``````and

int count_bits(int num){

int count = 0;
while(((num & 1) != 1)  && count < (sizeof(int) * 8){
num = num >> 1;
count++;
}
if(num & 1)
return count;
return -1;
}``````

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

``````#include <stdio.h>

int bitset(int n)
{
int i=1,pos=1;
for(;i<sizeof(int)*8;i*=2,pos++)
{
if((n&i)==i)
return pos;
}
return -1;
}

int main()
{
int n,pos;
printf("Enter no. to find its first set bit position from the right side: ");
scanf("%d",&n);
pos=bitset(n);
if(pos==-1)
printf("\nNo. is zero");
else
printf("\nFirst set bit position from the right side: %d",pos);
}``````

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

count =1;
while(((num|1) != num) && count++)
num = num>>1;
print count;

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

int i=0;
for(n;n>0;n/=2)
{i++;
if((n%2)==1)
break;
}
cout<<"position="<<i;

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

``````#include<iostream>
#include<math.h>
using namespace std;
int main(){
int a=2,count=0,num,flag=0;
cin>>num;
while(1){
if(num==0){
cout<<"No bit will set to 1:\n";
break;
}
else
{
flag=num & (int)pow(a,count);
if(flag!=0){
cout<<count+1<<"\n";
break;
}
count++;
}

}
return 0;
}``````

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