I'm trying to figure out the left binary shift operator (line 40), but what I understand is when I shift it to the left the compiler automatically adds a 0 onto the end instead of rolling the first number over. I'm having some trouble understanding how to get the number to rollover instead of just adding a 0. Any help would be very much appreciated.
void mystery()
{
ifstream inFile;
char mysteryIn[5000];
char keyEncrypt = 'X';
int solution[5000];
int solution2[5000];
inFile.open("mystery.dat",ios::in | ios::binary );
// Readin in File
if(inFile.fail())
{
cout << "File did not open!";
Sleep(2000);
exit(1);
}
else
{
for(int i=0; i< 5000;i++)
{
inFile >> mysteryIn[i];
}
}
//Decrypting Binary data
for (int j = 0; j < 5000; j++)
{
solution[j] = mysteryIn[j] ^= keyEncrypt;
}
//Reading out the first part of the mystery
for (int m=0; m<5000; m++)
{
cout << (char)solution[m];
}
//Shiftiing 1 binary space to left and reading into solutions2 variable
for (int l =0; l < 5000; l++)
{
solution2[l] = (solution[l] << 1);
}
// Reading Solutions2 and typcasing it by character
for (int k=0; k<5000; k++)
{
cout << (char)solution2[k] ;
}
}
Actually I didnt notice that it is solution2 and solution so you can't use operator <<
Also where is the wrap? I don't see it.
Are you saying something like you want to have a 1 byte number ( 8 bits ) shift to the left and if its going to turn into a 9 bit to roll over to the first bit?
So say we have 128 == 1000 0000 and you try and shift it left one you want it to wrap to the first bit instead so make it 0000 0001?
You could just subtract 255 :P ( the 9th bit will be 256 and you are adding one to the first bit so 256 - 1 = 255 )
Say we have the number 223 == 1101 1111
Now you are trying to shift to the left and get 0001 1011 1110 == 446 but since it is now one bit over 8 bit you want it to wrap to the first bit
now it is 1011 1111 == 191
A way to check would be
446 - 255 == 191
Here's a quick demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
constint wrap = 255;
int x = 1;
std::cout << "X = " << x << std::endl;
while( x < 256 )
{
x <<= 1;
std::cout << "X = " << x << std::endl;
}
if( x >= 256 ) //the next shift
{
x -= wrap;
std::cout << "X wrapped!" << std::endl << "X = " << x << std::endl;
}
giblit - Thank you so much for the help. Yes. I want to wrap when I shift to the left, but I dont know how to code it. I understand your code, but how can I append it into my code?
Good God I feel like my brain is frying right now.
just put an if statement after line 40 within that for loop. If you're using a character they are 2 bytes. So you could use pretty similar to what I posted.