Binary SHift

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.

Danka,

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` `````` 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] ; } }``````
Last edited on
shifting a unit to the left one bit is the same as multiplying it by 2

Lets look at an example

4 << 1 == 8

4 in binary == 0100

When we shift it to the left one bit it is now 1000

Binary is base 2 so moving it one to the left is the same as multiplying it by 2

Basically this a << b == a * 2b

*On a side note on line 40 you can also use the compound operator: <<= which would look like `solution2[l] <<= 1;`

Last edited on
What about a binary shift left with wrap?
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:

 ``12345678910111213141516`` `````` const int 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; }``````

http://ideone.com/oEnRe4
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.
so would would 'X' be solution[l]?
yes
Topic archived. No new replies allowed.