How do I leftshift an integer x amount to the left ??
I am bit confused on how i should do it??
It seems like an easy problem, but can't come up with an solution...
#include <iostream>
usingnamespace std;
int main()
{
int x = 24;
cout << x << endl;
// shift left by 2 examples
int y = x << 2;
cout << y << endl;
x <<= 2;
cout << x << endl;
// shift right by 2 examples
int z = x >> 2;
cout << z << endl;
x >>= 2;
cout << x << endl;
return 0;
}
Ok, I see what you mean now. It is not complicated, but the result will probably not be efficient, and quite long to write. If you can, I would suggest to have a look at the Standard Template Library. You could use list or vector to do that, instead of array.
But if you want to do it with array, here are the first steps that came to my mind:
Build an array of size N-1 from equal to the initial but without the number to be shifted.
Then rebuilding the size N array with a loop on the N-1 array, and an if condition inside the loop to insert the extracted value at the proper place.