NVIDIA Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Could you explan me....what is happening here...
((ch & 0xAA)>>1 ) | ( (ch & 0x55) << 1)
Please explain in details
Thanks a ton in Advance
@Raj: Use a binary calculator to see what the 0x55 and 0xAA look like in binary.
1. Mask out all the even bits in the char: ch & 0xAA
2. Mask out all the odd bits in the char: ch & 0x55
Now you have two values - the masked odd and even bits of the original number. So, all you have to do now is to use a biwise OR operation to combine these two values, having shifted by 1 position to the left and to the right the respective temp numbers.
Hope that answered your question.
@TheWofle :The dependency is just due to the size of integer.In gcc its 32 bit so above mentioned code will not work on it it will swap only 8 bits. So for gcc you can modify code as
return ( ((ch & 0xaaaaaaaa) >> 1) | ((ch & 0x55555555) << 1) );
#include<stdio.h>
int main()
{
int n=10;
int m;
m=n;
int count=0;
while(m)
{
count++;
m=m>>1;
}
printf("Value before swapping:%d\n",n);
printf("No of Bits:%d",count);
int i=0;
int k=1;
while(k<=count/2)
{
int a=1;
int b=1;
a=((a<<i)&n)>>i;
b=((b<<(i+1))&n)>>(i+1);
//a=a&n;
//b=b&n;
//a=a>>i;
//b=b>>(i+1);
int c=a^b;
int d=(c<<i)|(c<<(i+1));
n=n^d;
i=i+2;
k++;
}
printf("\nValue after Swapping Bits:%d",n);
return 0;
}
Output:
Value before swapping:10(1010)
No of Bits:4
Value after Swapping Bits:5(0101)
----------------------------------
Value before swapping:21(10101
No of Bits:5
Value after Swapping Bits:26(11010)
---------------------------------
Value before swapping:15(1111)
No of Bits:4
Value after Swapping Bits:15(1111)
Done for integer
May be this can be true for integer also.
Replacing char n='b';and int m with char m;gives following output.
Value before swapping:98 b
No of Bits:7
Value after Swapping Bits:81 Q
A little long, but works generically for larger sizes. Mask is constructed dynamically.
flip_adj (int value)
{
int i, mask, temp;
for (i = 0; i < (sizeof(int)*8); i = i+2) {
mask = 0;
mask|= (1<<i) | (1<<(i+1));
temp = value;
temp = temp & mask;
if (((temp >> i) & 1) != ((temp >> (i+1)) & 1)) {
temp = ~temp;
}
temp = temp & mask;
value = value & (~mask);
value = value | temp;
}
printf("\nAfter flip = %#x", value);
}
main ()
{
int value = 0xaa; // 1010 1010
printf("\nValue = %#x", value);
flip_adj(value);
}
Running program:
# Value = 0xaa (1010 1010)
# After flip = 0x55 (0101 0101)
void showbits(char);
void swapbits(char,int,int);
void main()
{
int i,j;
char ch='A';
printf("\n input the character\n");
scanf("%c",&ch);
printf("character is %c and its binary equivalent is \n",ch);
showbits(ch);
printf("\n After swaping..");
for(j=7;j>=0;)
{
swapbits(ch,j,j-1);
j=j-2;
}
printf("\n");
getch();
}
void showbits(char ch )
{
int i,k,andmask;
for(i=7;i>=0;i--)
{
andmask=1<<i;
k=ch&andmask;
k==0?printf("0"):printf("1");
}
}
void swapbits(char ch,int j,int l )
{
int i,k,andmask;
andmask=1<<l;
k=ch&andmask;
k==0?printf("0"):printf("1");
andmask=1<<j;
k=ch&andmask;
k==0?printf("0"):printf("1");
}
#include<stdio.h>
#include<conio.h>
int binary(int);
int swap(int);
int in[100];
int main()
{
unsigned char c;
printf("enter any character=");
scanf("%c",&c);
int d,i,count;
d = (int)c;
printf("%d\n",d);
count = binary(d);
for(i = count;i>=0;i--)
printf("%d",in[i]);
swap(count);
printf("\n\n");
for(i = count;i>=0;i--)
printf("%d",in[i]);
getch();
return 0;
}
int binary(int num)
{
int j=0;
while(num!=1)
{
in[j] = num%2;
num = num/2;
j++;
}
in[j] = 1;
return j;
}
int swap(int a)
{
int i,j;
for(i=a;i>=0;i--)
{
if(i==0)
{}
else{
j = in[i];
in[i] = in[i-1];
in[i-1] = j;
i--;
}}
return 0;
}
mask1=0XFF;
mask2=0XFF00;
int byte1 = (number & mask1)<<8;
int byte2 = (number & mask2)>>8;
int newumber = byte1 + byte2;
* You can extend the above logic to extend to the whole unsigned number .
* Just generate the masks accordingly
@salvo: This does not do what is required - swap every two bits. Instead, it does two lower BYTES.
This is basically swapping odd and even bits, which is done using bitmasks. Implementation provided below:
- ashot madatyan May 16, 2012