Qualcomm Interview Question






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

clear(int x,int n)
{
int k=1;
int mask=0;
mask=k<<(n-1);
x=x & (~mask);
return x;
}

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

k = 1;
for(int i = 0; i < n; i++)
k<<1;
m = k ^ 0;
input = m & input;

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

int mask =0 ;

mask | = 1<<(n-1);

x= x&(~mask);

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

int mask =0 ;

mask | = 1<<(n-1);

x= x&(~mask);

In the above solution, in place of "mask | = 1<<(n-1);" we can simply keep "mask = <<(n-1);". There is no need of using |=. Please correct me if I am wrong.

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

int mask =0 ;

mask | = 1<<(n-1);

x= x&(~mask);

In the above solution, in place of "mask | = 1<<(n-1);" we can simply keep "mask = 1<<(n-1);". There is no need of using |=. Please correct me if I am wrong.

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

return nInput&(~(1<<(n-1)));

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

how about
mask=1;
mask<<n-1;
number=(number&mask)?(number^mask):number

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

int clear(int n, int num)
{
    num = 1<<(n-1) ^ num;
    return num;
}

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

Sorry, I also wanted to add the condition to check if the bit=0, then there's no need to clear.
int clear(int n, int num)
{
int newnum = 1<<(n-1) ^ num;
if(num & (1<<(n-1)) == 0)
return num;
return newnum;
}

- Dee April 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

All of these solutions are of order n where n is the bit to be cleared. This can, however, be done in constant time with a lookup table. Build a table (an array) that has a preset bitmask for each bit in the integer. The 'Clear' function then looks up the mask using the value of n passed in. The mask is then applied and the modified value of x returned.

O(n) is too much for a function like this that may be used many times. The lookup table solution is O(1).

- Wandering programmer April 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your solution looks good when we use it many times.

- yours.ramesh February 11, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int clear_n(int num,int n)
{
return (num = num&(~(1<<(n-1)));
}

- padhu September 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

n ^ (1<<(n-1))

- vatz May 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Code:

public static int clearNthBit(int num, int nthBit){
		if(num == 0)
			return num;
		
		int k = 1<<(nthBit-1);
		if((num & k) >= 1){
			return num ^ k; //bit is set clear the bit
		} else {
			return num; //bit is clear
		}
	}

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

return ip&(~0-pow(2,n));

- anonymous August 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

return ip&(~0-pow(2,n));

- anonymous August 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

num &= (~0<<n)

num is integer
n bit want to disable

please correct me if im wrong

- sasi123789 November 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x=number/(10^n); //quotient
number=number-x*(10^n);

- noob October 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

k=size(number) - n;
array x[k];
x[k]=number/(10^n);
number=number-*(10^n);

- u missed it October 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.lang.*;
import java.io.*;
import java util.*;
class toggle_n{
int n,b=1;

System.out.println("enter an number");
Scanner sc=new Scanner();
int a=sc.nextInt();

System.out.println("enter position to be toggle");
n=sc.nextInt();

b=b<<(n-1);
a=a^b;

System.out.println("number after toggling nth bit");
System.out.println(a);

}

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

//question 5 of java queston is to toggle nth bit 
import java.io.*;
import java.util.*;
import java.math.*;

   class toggle{

              public int i,j;
              int index=0,id=0;
              int binary[],binary2[];

              void toggling_nbit()throws IOException
                 {
                     System.out.println("ENTER A NUMBER ");
                     String s;
                     String p=" ";
                     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                     s=br.readLine();
                     int a=Integer.parseInt(s);       
                      binary = new int[40];
                      tobinary(a);
            
              System.out.println( ); 
              System.out.println("WHICH BIT U WANT TO TOGGLE");
                    s=br.readLine( );
                     int n=Integer.parseInt(s);
                     int b=1;
                     int c=0;
             System.out.println( );   
                     for(i=1;i<=n-1;i++)
                       {       System.out.println( ); 
                                System.out.println("SHIFT FACTOR" );      
                                System.out.println(c=(b<<i));
                       }
         
                    binary2=new int[40];
                    id = 0;
 
                           while(c> 0)
                             {
                                   binary2[id++] = c%2;
                                   c = c/2;
                              }

                           /*for( i = id-1;i >= 0;i--)
                                {        
                                  System.out.print(binary2[i]);
                                 }*/
            System.out.println( );   
                        xor(binary,binary2);

        }//toggling function closed
          
                void tobinary(int a)
                  {
                           while(a > 0)
                           {
                                binary[index++]= a%2;
                                  a=a/2;
                            }
                        System.out.println(" BINARY VALUE OF INPUT" );   

                           for( i = index-1;i >= 0;i--)
                              {       
                               System.out.print(binary[i]);
                              }

                  
                    }//to binary function vclosed


        
       void xor(int binary[],int binary2[])
        {
                  System.out.println("VALUE AFTER TOGGLE");
                  for(i=0;i<=index-1;i++)
                   {
                         if(binary[i]==binary2[i])
                               {binary[i]=0;}
                    
                           else
                          { binary[i]=1;}
                     }


                 for( i = index-1;i >= 0;i--){        //binary of input
                 System.out.print(binary[i]);
                 }

         }
     
   }//class closed


  class toggle_n{
           public static void main(String args[]) throws IOException{
              toggle t =new toggle( );
              t.toggling_nbit( );
              }//main closed

             }//class closed

- ishika raizada August 12, 2016 | 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