Can anyone tell me what this function is doing? (The code has been written in C,
ID: 3872302 • Letter: C
Question
Can anyone tell me what this function is doing? (The code has been written in C, and we assume that we have 32 bit integers).
int myFunction(int x) {
int mask1, mask2, mask4, mask8, mask16;
mask2 = 0x33 + (0x33 << 8);
mask2 += mask2 << 16;
mask1 = mask2 ^ (mask2 << 1);
mask4 = 0x0F + (0x0F << 8);
mask4 += mask4 << 16;
mask8 = 0xFF + (0xFF << 16);
mask16 = 0xFF + (0xFF << 8);
x = (x & mask1) + ((x >> 1) & mask1);
x = (x & mask2) + ((x >> 2) & mask2);
x = (x & mask4) + ((x >> 4) & mask4);
x = (x & mask8) + ((x >> 8) & mask8);
x = (x & mask16) + ((x >> 16) & mask16);
return x;
}
Explanation / Answer
Working of function is given in comments below ------->
int myFunction(int x) {
int mask1, mask2, mask4, mask8, mask16;
//0x33(16) = 51(10) = 110011(2)
mask2 = 0x33 + (0x33 << 8);//110011 left shift by 8 so it become 11001100000000(2) = 13056(10); 51+13056 = 13107
mask2 += mask2 << 16;//mask2 = mask2 + (mask2 left shifted by 16)
mask1 = mask2 ^ (mask2 << 1);//mask 1 = mask2 ^ (mask 2 left shift by 1)
mask4 = 0x0F + (0x0F << 8);//0x0F(16) = 1111(2); mask 4 = 1111 + (1111 left shift by 8)
mask4 += mask4 << 16;//mask4 = mask4 + (mask4 left shift by 16)
mask8 = 0xFF + (0xFF << 16);//0xFF(16)=11111111(2); mask8 = 11111111 + (11111111 left shift by 16)
mask16 = 0xFF + (0xFF << 8);//mask16 = 11111111 + (11111111 left shift by 8)
//& is bitwise and; >> is right shift
x = (x & mask1) + ((x >> 1) & mask1);//(bitwaise and between x and mask1) + (bitwise and between (x right shift by 1) and mask1)
x = (x & mask2) + ((x >> 2) & mask2);//(bitwaise and between x and mask2) + (bitwise and between (x right shift by 2) and mask2)
x = (x & mask4) + ((x >> 4) & mask4);//(bitwaise and between x and mask4) + (bitwise and between (x right shift by 4) and mask4)
x = (x & mask8) + ((x >> 8) & mask8);//(bitwaise and between x and mask8) + (bitwise and between (x right shift by 8) and mask8)
x = (x & mask16) + ((x >> 16) & mask16);//(bitwaise and between x and mask16) + (bitwise and between (x right shift by 16) and mask16)
return x;
}
To know better about this function try this program main.cpp -------->
#include <iostream>
using namespace std;
int myFunction(int x) {
int mask1, mask2, mask4, mask8, mask16;
cout<<"mask1 = "<<mask1<<"; mask2 = "<<mask2<<"; mask4 = "<<mask4<<"; mask8 = "<<mask8<<"; mask16 = "<<mask16;
cout<<"; 0x33 = "<<0x33<<endl;//0x33(16) = 51(10) = 110011(2)
mask2 = 0x33 + (0x33 << 8);//110011 left shift by 8 so it become 11001100000000(2) = 13056(10); 51+13056 = 13107
cout<<"mask2 = "<<mask2<<endl;
mask2 += mask2 << 16;//mask2 = mask2 + (mask2 left shifted by 16)
cout<<"mask2 = "<<mask2<<endl;
mask1 = mask2 ^ (mask2 << 1);//mask 1 = mask2 ^ (mask 2 left shift by 1)
cout<<"mask1 = "<<mask1<<endl;
mask4 = 0x0F + (0x0F << 8);//0x0F(16) = 1111(2); mask 4 = 1111 + (1111 left shift by 8)
cout<<"mask4 = "<<mask4<<endl;
mask4 += mask4 << 16;//mask4 = mask4 + (mask4 left shift by 16)
cout<<"mask4 = "<<mask4<<endl;
mask8 = 0xFF + (0xFF << 16);//0xFF(16)=11111111(2); mask8 = 11111111 + (11111111 left shift by 16)
cout<<"mask8 = "<<mask8<<endl;
mask16 = 0xFF + (0xFF << 8);//mask16 = 11111111 + (11111111 left shift by 8)
cout<<"mask16 = "<<mask16<<endl;
//& is bitwise and; >> is right shift
x = (x & mask1) + ((x >> 1) & mask1);//(bitwaise and between x and mask1) + (bitwise and between (x right shift by 1) and mask1)
cout<<"x = "<<x<<endl;
x = (x & mask2) + ((x >> 2) & mask2);//(bitwaise and between x and mask2) + (bitwise and between (x right shift by 2) and mask2)
cout<<"x = "<<x<<endl;
x = (x & mask4) + ((x >> 4) & mask4);//(bitwaise and between x and mask4) + (bitwise and between (x right shift by 4) and mask4)
cout<<"x = "<<x<<endl;
x = (x & mask8) + ((x >> 8) & mask8);//(bitwaise and between x and mask8) + (bitwise and between (x right shift by 8) and mask8)
cout<<"x = "<<x<<endl;
x = (x & mask16) + ((x >> 16) & mask16);//(bitwaise and between x and mask16) + (bitwise and between (x right shift by 16) and mask16)
cout<<"x = "<<x<<endl;
return x;
}
int main()
{
myFunction(10);
return 0;
}
Output ------->
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.