HCL Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

The only thing I can come up with is reversing bit by bit. I don't think there's any good binary operation for this.

unsigned int a = 100; //just treat a as your binary number, 
unsigned int b = 0; // the final result
int s = sizeof(a) * CHAR_BIT; // # of bits in a;

for(int i = 0; i < s; i++)
{
	b <<=1; // left shift b
	b |= a & 0x1; //get unit bit
	a >>= 1; // right shift a
}
//now b is your result

The logic behind this is:

Let's say
a=1010

Step 1:
a=0101
b=0001
Step 2:
a=0010
b=0010
etc.

- Mo Lam May 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Now that I think more about it,
first position's output depends on last position's input,

--> We can't do it with simple AND/OR/NOT (XOR, and anything form by the 3 basic)
--> We need to some how have shifted 1st pos to last, 2nd to (2nd to last).

--> My ways work, but it's probably more efficient if we shift alternate bits >> 1 << 1, then alternate 2 bits >>2 <<2, etc. (issue with this is we don't know the size of the binary number, can't be sure that it's power of 2).

- Mo Lam May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

void Reversebit(int n)
{
int i=0,j=0;
while(i<16)
{
//printf("\n pos %d=%d pos %d = %d",i,(n & (1<<i)),31-i,(n & (1<<(31-i))));
if((n & (1<<i)) != (n & (1<<(31-i)))){/*Check if i & 32-i th bits are not same ,
then just toggle both the bits*/
n ^= (1<<i);
n ^= (1<<(32-i));
}
i++;
}
binary(n); /*print the number in binary form*/
}

- ashu.mohan07 November 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

google for bit twiddling hacks.

int main()
{
        int x = 0x000000F0;

        x = ((x&0xAAAAAAAA) >> 1)| ((x& 0x55555555)<<1);
        x = ((x&0xCCCCCCCC) >> 2)| ((x& 0x33333333)<<2);
        x = ((x&0xF0F0F0F0) >> 4) | ((x& 0x0F0F0F0F)<<4);
        x = ((x&0xFF00FF00) >> 8) | ((x& 0x00FF00FF)<<8);
        x = x >> 16 | x<< 16;
        printf("%x\n", x);
}

- aka May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Consider the following bit pattern:
ABCDEFGHIJKLMNOP

How to reverse it??
First reverse a group of 1 bit with adjacent group of 1 bit...
BADCFEHGJILKNMPO

Then reverse a group of 2 bits with adjacent group of 2 bits...
DCBAHGFELKJIPONM

Then reverse a group of 4 bits with adjacent group of 4 bits...
HGFEDCBAPONMLKJI

Then reverse a group of 8 bits with adjacent group of 8 bits...
PONMLKJIHGFEDCBA

Keep doing this till group size becomes sizeof(bit pattern) / 2... :)

- coding.arya May 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@coding.arya: Probably you did not read the question correctly. You have to use bitwise operator..

- alex May 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is bitwise, the only issue is, what if the binary number is 24 bit for some reason?

- Mo Lam May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Mo Lam: Just use sizeof operator to find out the size and use if else to distinugish which reverse operation you need.

- aka May 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

#include <stdio.h>

int reverse_bits(int n){
  return n^(~0);
}

void print_bits(int x) {
  int wl = sizeof(int)*2-1;
  unsigned mask;
  for (mask = 1<<wl; mask; mask >>= 1)
    putchar(x&mask ? '1' : '0');
  putchar('\n');
}

int main(){
  
  int n = 361;
  print_bits(n);

  n = reverse_bits(361);
  print_bits(n);

  return 0;
}

- srdjan May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

n^(~0) gives inverse, not reverse

- Mo Lam May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

return ~(n&(~0)); //This works for +ve and -ve

- Jega June 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdio.h"
main()
{
int a,b=0;
int temp=1;
printf("Enter a");
scanf("%d",&a);
while (a>0)
{
b|=(a&temp);
a=a>>1;
if (a)
b=b<<1;
}
printf("reversed number is %d",b);
}

- Anonymous May 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

What logic have you used? Please explain.

- alex May 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are issues. What if a = 10000.....001 (a is negative int).
And it is <stdio.h>

- Mo Lam May 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
#define size(arr) sizeof(arr)/sizeof(arr[0])
int main(){
        int arr[]               = {1,0,0,0,1,0,1,1,1,0,1,1,0,1,0,0,1}, index=0,index_1=0;
        int num_of_elems        = size(arr);
        int *result             =malloc(num_of_elems*sizeof(int));
        printf("Input : ");
        for(;index <num_of_elems;index++){
                printf("%d ", arr[index]);
                if(1 == arr[index] )
                        result[num_of_elems-index-1]    = arr[index];
        }
        printf("\nOutput : ");
        for (index=0;index<num_of_elems;index++)
                printf("%d ", result[index]);
        printf("\n");
        return 0;
}

- Angamuthu G May 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void main()
{
    int i=0,temp=1,a=0;
    int x;
    scanf("%d",&x);
    
i=0;
while(i<16)
{
if(x&temp)
{
   a=a|1;
   a=a<<1;
}
else
{
  a=a<<1;
}
x=x>>1 ;
i++ ;
}
printf("%d",a);
}

- aravind May 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what the hell is this

- anon February 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//This in my opinion is best solution as I love CODING...
reverseBinary(int num){
int rev = 0;
//Considering num is 4 bytes
for(int i=0;i<4*8;i++){
rev = (rev<<1) | (num&1);
num = num>>1;
}
return rev;
}

- loveCoding May 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do
{
if(x&1)
{
printf("1");
}
else
{
printf("0");
}
while(x>>=1);
}

- Sachin Vani September 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main()
{
int a,i,j,ar[10000001];
cin>>a;
for(i=0;i<a;i++)
{
cin>>ar[i];
}
for(j=0;j<a;j++)
{
int b=1;
int temp=1;

while (ar[j]>0)
{
b|=((ar[j])&temp);
ar[j]=(ar[j])>>1;
if (ar[j])
b=b<<1;
}
cout<<b<<endl;
}
return 0;
}


Why the above code show mw runtime error , could anyone help me please
thanks...

- Anonymous November 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
unsigned char swapBits(unsigned char n, unsigned char p1, unsigned char p2)
{
return (((n >> p1) & 1) == ((n >> p2) & 1) ? n : ((n ^ (1 << p2)) ^ (1 << p1)));
}

int main()
{
unsigned char n = 2; //any given binary number
unsigned char p1 = 0; //First bit position
unsigned char p2 = sizeof(n) * 8 - 1;//last bit position
while (p1++ < p2--)
{
n = swapBits(n, p1, p2);
}
printf("%d", n);
return 0;

}

Note : This can be done using recursion in shorter way, Try it!

- parth November 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
unsigned char swapBits(unsigned char n, unsigned char p1, unsigned char p2)
{
    return (((n >> p1) & 1) == ((n >> p2) & 1) ? n : ((n ^ (1 << p2)) ^ (1 << p1)));
}

int main()
{
unsigned char n = 2; //any given binary number
unsigned char p1 = 0; //First bit position
unsigned char p2 = sizeof(n) * 8 - 1;//last bit position
while (p1++ < p2--)
{
  n = swapBits(n, p1, p2);
}
printf("%d", n);
return 0;

}

This can be done using recursion in shorter way! try out.

- parth November 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

100001101010100101010100111000000011000000111001

- Anonymous June 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
unsigned char swapBits(unsigned char n, unsigned char p1, unsigned char p2)
{
return (((n >> p1) & 1) == ((n >> p2) & 1) ? n : ((n ^ (1 << p2)) ^ (1 << p1)));
}

int main()
{
unsigned char n = 2; //any given binary number
unsigned char p1 = 0; //First bit position
unsigned char p2 = sizeof(n) * 8 - 1;//last bit position
while (p1++ < p2--)
{
n = swapBits(n, p1, p2);
}
printf("%d", n);
return 0;

}

- Anonymous June 22, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Dont know how exactly you want it in c but i think that you have to do the following operation.

number = number xor 11111111

- Michail May 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This gives you the inverse, not the reverse.

- alex May 11, 2013 | Flag


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