Hi there
I am going to write a binary code.
I don't know how to use binary shift. my code looks like this :
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<iostream>
usingnamespace std;
int main(){
int a,b;
a=9; // a is 1001
b= a <<1; // b is 10010
cout<< b;
return 0;
}
i want my number (here a=9 =1001) after shift become (b=3=0011) not 18=10010.
I think I have to use sth like register or something like that. however i don't how to use that?
Dear Athar
I just wrote a code to learn how it work and use it in my own code which is so big that I think it is boring to bring all of that with explanation.
@hamsterman
I'd like to use it for more than 10 bits and it is difficult to use function. can you explain a little more about rol/ror or inline assembly or put a tutorial.
What Athar meant was that you don't say what you want clearly. There is a billion ways to turn 9 into 3. I'm amazed that Disch managed to deduce it.
I'd like to use it for more than 10 bits
With ror/rol (or any other instruction) you can't choose any number of bits you want. There are versions for 8, 16, 32 bits. If you want others, you'll have to do (almost) what Disch did. Almost, because that function only shifts one bit and has an unnecessary jump.
As for using inline asm, it depends on your compiler.