HCL Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
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).
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*/
}
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);
}
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: Probably you did not read the question correctly. You have to use bitwise operator..
this is bitwise, the only issue is, what if the binary number is 24 bit for some reason?
#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;
}
#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);
}
#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;
}
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);
}
#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...
#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!
#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.
#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;
}
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.
The logic behind this is:
- Mo Lam May 15, 2013Let's say
a=1010
Step 1:
a=0101
b=0001
Step 2:
a=0010
b=0010
etc.