Google Interview Question for Software Engineer / Developers






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

swap(int n,int i,int j)
{
if( (n & 1<<i)>>i ^ (n & (i<<j))>>j) ) // if bits i and j are different
{
n^= 1<<i;
n^= 1<<j;
}
return n;
}

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

excellent! thanks,

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

#include<stdio.h>
main()
{
int n, i, j, s;
printf("enter values of n, i and j");
scanf("%d%d%d",&n,&i,&j);
s = swap(n,i,j);
printf("%d",s);
}
swap(int n,int i,int j)
{
if(i!=j)
{
n^= 1<<i;
n^= 1<<j;
}
return n;
}

- V: August 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm not sure it's necessary to check for i != j. It would still be correct if i == j; you don't need to incur the cost of a branch.

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

{
void swap(int&n, int i, int j) {
int xori = (((1 << i) & n ) >> i ) ^ (((1 << j) & n ) >> j );
n ^= xori << i;
n ^= xori << j;
}
}

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

void swap(int&n, int i, int j) {
    n ^= !((1<<i)&n)^!((1<<j)&n);
}

- Bullocks July 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My bad. That was incorrect. Please ignore the last comment.

- Bullocks July 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I do not understand how this solution works out.
let n = 01'1'1 01'1'0
i = 6,j=2 ( quoted those two bit positions - bit position starting from 1 from left end).

n^=(1<<i)
1<<6 => 0100 0000
n = n^(1<<6) = 0011 0110

n=n^(1<<j) = n^(1<<2)
1<<2 => 0000 0100
n = n^(1<<2) = 0011 0010

- Krishna December 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

little addition of simplicity

int temp = ((n & (1 <<i)) | (n & (1 <<j)))
if (temp & temp-1==0) //bits are different
{
n^=1<<i;
n^=1<<j;
}

- Manju January 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Swapping individual bits with XOR 
unsigned int i, j; // positions of bit sequences to swap
unsigned int n;    // number of consecutive bits in each sequence
unsigned int b;    // bits to swap reside in b
unsigned int r;    // bit-swapped result goes here


unsigned int x = ((b >> i) ^ (b >> j)) & ((1U << n) - 1); // XOR temporary
r = b ^ ((x << i) | (x << j));

- Ashupriya July 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

check it out...
graphics.stanford.edu/~seander/bithacks.html#SwappingValuesSubAdd

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

int d=5,i=2,j=1;
int tmp1=d;
d=d&(0xFFFFFF^1<<i);
d=d&(0xFFFFFF^1<<j);
d=d+(pow(2,i)*((tmp1>>j)&1))+(pow(2,j)*((tmp1>>i)&1));
cout<<d;

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

You can't be serious. Pow???

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

/* Swap bits "i" and "j" inside "num" and return the result.
 * The idea used here is from how to swap two integers using exclusive-ORs. */

int swapBits(int num, int i, int j)
{
  int temp ;
  if ((i >= 0) && (i < 8*sizeof(int)) && (j >= 0) && (j < 8*sizeof(int)) && (i^j))
  {
    temp = (num >> i) ^ (num >> j) & 0x01 ;
    num ^= temp << j ;
    num ^= temp << i ;
  }
  return (num) ;
}

- Venky May 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Apparently, it is not correct!

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

int Swapbits(int num, int pos1, int pos2)
{
int temp1 = num>>(pos1-1) & 1;
int temp2 = num>>(pos2-1) &1;
int swapnum = num;
//int temp3 = 0;

if(temp1 ^ temp2)
{
//bits are different
int temp3 = 1<<(pos1-1);
int temp4 = 1<<(pos2-1);
swapnum = num ^(temp3 | temp4);
}
return swapnum;
}

int _tmain(int argc, _TCHAR* argv[])
{
int num = 102;
int newnum = Swapbits(num, 3,6);

return 0;
}

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

void swap(int *a,int i,int j)
{
int p = *a;
unsigned int k = 1;
#if 0
*a = ((p&(~(k<<(i-1))))& (p&(~(k<<(j-1)))) )| (k<<(j-1) & (p <<(j -1))) | (k<< (i - 1) & p>>(j - i));
#endif
if((p>>(j - 1) & 1) == 0x01)
{
p = p&(~(k<<(j-1)));
}
else
{
p = p | (1<<j-1);
}
if((p>>(i-1) & 1) == 0x01)
{
p = p&(~(k<<(i-1)));
}
else
{
p = p | (1<<i-1);
}
*a = p;

}

main()
{
int a,i,j;
printf("enter a,i,j\n");
scanf("%d%d%d",&a,&i,&j);
swap(&a,i,j);

printf("%d\n",a);
swap(&a,i,j);
printf("%d\n",a);

}

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

All that just to swap two bits? C'mon guys this is depressingly amusing.

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

int swapbits(int num, int i, int j)
{
	if(((num>>i&1)==1 && (num>>j&1)==1) || ((num>>i&1)==0 && (num>>j&1)==0))
		return num;
	num ^= 1<<i;
	num ^= 1<<j;
	return num;
}

- limaye.nikhil November 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This assumes j > i; printfs for clarity.

bitswap( unsigned char i, unsigned char j, unsigned int& num )
{

unsigned int mask = 0x00000001;

// Mask to turn off ith and jth position
unsigned int m1 = mask << (i-1); printf( "m1: %x\n", m1 );
unsigned int m2 = mask << (j-1); printf( "m2: %x\n", m2 );

unsigned int sw1 = (m1 & num) << (j-i);
unsigned int sw2 = (m2 & num) >> (j-i);

// Turn OFF ith and jth position in the original num, and OR with swapped bits.
unsigned int val = ( ~(m1 | m2) & num ) | sw1 | sw2;

printf( "num: %X\n", num );
printf( "val: %X\n", val );

num = val;

}

- Jan January 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Jan: it has to be
m1=mask<<i;
m2=mask<<j;
and not i-1 and j-1
with these modifications i got correct answer on devc++

- shiv4289 June 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

unsigned swap_bits(unsigned x, int i, int j) {
unsigned y = ((x & (1 << i)) >> i) << j;
unsigned z = ((x & (1 << j)) >> j) << i;
x &= ~((1 << i) | (1 << j));
x |= y|z;

return x;
}

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

String s;
	int x = 23456;
	int loc1 = 4;
	int loc2 = 7;
	int j = 1;
	j = j << (loc2-loc1);
	j = j+1;
	j = j << loc1-1;
	x = x^j;
	s = Integer.toBinaryString(x);
	System.out.println(s);

- CAFEBABE December 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int setBit(int num, int i, boolean value){
		if (value == true){
			return (1 << i) | num;
		}else{
			return (~(1 << i)) & num;
		}
	}
	
	public static boolean getBit(int num, int i){
		return (((1 << i) & num) != 0);
	}
	
	public static int swapBits(int num, int i, int j){
		boolean bitI = getBit(num, i);
		boolean bitJ = getBit(num, j);
		boolean temp = bitI;
		num = setBit(num, i, bitJ);
		num = setBit(num, j, temp);
		return num;
	}

- Iman July 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int swapBits(int n, int i, int j){
	int mask1 = 1 << i;
	int mask2 = 1 << j;
	
	// Get bits
	int bit1 = ( n & mask1 ) >> i;
	int bit2 =  ( n & mask2 ) >> j;
	
	// Clear bits
	n = n & ~mask1;
	n = n & ~mask2;
	
	// Put each bits 
	n = n | bit1 << j;
	n = n | bit2 << i;		
	
	return n;
}

- tgi01054 September 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class HelloWorld{

public int swap(int num,int i,int j)
{
int lowcheck,highcheck,hcheck,lcheck,partial,mask,intermediate;
lowcheck=1;
highcheck=1;
lowcheck<<=j;
lcheck=lowcheck;
highcheck<<=i;
hcheck=highcheck;
lowcheck=num&lowcheck;
highcheck=num&highcheck;
highcheck>>>=(i-j);
if (lowcheck==highcheck)
System.out.println ("The bits are same");
else
{
lowcheck<<=(i-j);
partial=lowcheck|highcheck;
mask=~lcheck&~hcheck;
intermediate=mask&num;
num=intermediate|partial;

}
return num;
}

public void start()
{
int num,i,j,s;
num=10;
i=3;
j=0;
s=swap(num,i,j);
System.out.println(s);
}


public static void main(String []args){
HelloWorld hw=new HelloWorld();
hw.start();
}
}

- vinit July 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class HelloWorld{

public int swap(int num,int i,int j)
{
int lowcheck,highcheck,hcheck,lcheck,partial,mask,intermediate;
lowcheck=1;
highcheck=1;
lowcheck<<=j;
lcheck=lowcheck;
highcheck<<=i;
hcheck=highcheck;
lowcheck=num&lowcheck;
highcheck=num&highcheck;
highcheck>>>=(i-j);
if (lowcheck==highcheck)
System.out.println ("The bits are same");
else
{
lowcheck<<=(i-j);
partial=lowcheck|highcheck;
mask=~lcheck&~hcheck;
intermediate=mask&num;
num=intermediate|partial;

}
return num;
}

public void start()
{
int num,i,j,s;
num=10;
i=3;
j=0;
s=swap(num,i,j);
System.out.println(s);
}


public static void main(String []args){
HelloWorld hW=new HelloWorld();
hW.start();
}
}

- vinit July 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class HelloWorld{

public int swap(int num,int i,int j)
{
    int lowcheck,highcheck,hcheck,lcheck,partial,mask,intermediate;
    lowcheck=1;
    highcheck=1;
    lowcheck<<=j;
    lcheck=lowcheck;
    highcheck<<=i;
    hcheck=highcheck;
    lowcheck=num&lowcheck;
    highcheck=num&highcheck;
    highcheck>>>=(i-j);
    if (lowcheck==highcheck)
    System.out.println ("The bits are same");
    else
    {
        lowcheck<<=(i-j);
        partial=lowcheck|highcheck;
        mask=~lcheck&~hcheck;
        intermediate=mask&num;
        num=intermediate|partial;
        
        }
    return num;
}

public void start()
{
    int num,i,j,s;
    num=10;
    i=3;
    j=0;
    s=swap(num,i,j);
    System.out.println(s);
}


     public static void main(String []args){
    HelloWorld hW=new HelloWorld();
    hW.start();
     }
}

- vinit July 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class HelloWorld{

public int swap(int num,int i,int j)
{
int lowcheck,highcheck,hcheck,lcheck,partial,mask,intermediate;
lowcheck=1;
highcheck=1;
lowcheck<<=j;
lcheck=lowcheck;
highcheck<<=i;
hcheck=highcheck;
lowcheck=num&lowcheck;
highcheck=num&highcheck;
highcheck>>>=(i-j);
if (lowcheck==highcheck)
System.out.println ("The bits are same");
else
{
lowcheck<<=(i-j);
partial=lowcheck|highcheck;
mask=~lcheck&~hcheck;
intermediate=mask&num;
num=intermediate|partial;

}
return num;
}

public void start()
{
int num,i,j,s;
num=10;
i=3;
j=0;
s=swap(num,i,j);
System.out.println(s);
}


public static void main(String []args){
HelloWorld hW=new HelloWorld();
hW.start();
}
}

- kallianpur.vinit July 12, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int swap_bits(int num, int i, int j) {
if ((num>>i & 0x1) ^ (num>>j & 0x1)) {
return ((num ^ 1<<i) & ~(1<<j)) | ((num ^ 1<<j) & ~(1<<i));
} else return num;
}

- Toseef K April 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The simple code for swapping the two bits of a given integer will be to iterate to the given two bits and store them in a variable as (x>>i) & 1 and the other as (x>>j) & 1 now, we will xor the two bits and store in a variable as x = (b1 ^ b2) and the, we will put back the bits in their place as x = (x<<i) | (x<<j) and at the end return the xor of the number and x to swap the two bits finally

Implementation:

int swapp(unsgned int n, unsigned i, unsigned int j){
unsigned int p1 = (n<<i) & 1;
unsigned int p2 = (n<<j) & 1;
unsigned int x = (p1^p2);
x = (n<<p1) | (n<<p2);
return n^x;

}

- swapnilkant11 July 22, 2019 | 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