0

Preferably for this to be done in C#.

Supposedly, I have an integer of 1024. I will be able to generate these equations:

4096  >> 2 = 1024  
65536 >> 6 = 1024
64    << 4 = 1024 

and so on...

Any clues or tips or guides or ideas?

Edit: Ok, in simple terms, what I want is, for example...Hey, I'm giving you an integer of 1024, now give me a list of possible bit-shift equations that will always return the value of 1024.

Ok, scratch that. It seems my question wasn't very concise and clear. I'll try again.
What I want, is to generate a list of possible bit-shift equations based on a numerical value. For example, if I have a value of 1024, how would I generate a list of possible equations that would always return the value of 1024?

Sample Equations:

4096  >> 2 = 1024  
65536 >> 6 = 1024  
64    << 4 = 1024 

In a similar way, if I asked you to give me some additional equations that would give me 5, you would response:

3  + 2 = 5  
10 - 5 = 5  
4  + 1 = 5

Am I still too vague? I apologize for that.

3 Answers3

0

You may reverse each equation and thus "generate" possible equations:

1024 >> 4 == 64 

and therefore

64 << 4 == 1024

Thus generate all right/left shifts of 1024 without loosing bits due to overflow or underflow of your variable and then invert the corresponding equation.

Howard
  • 38,639
  • 9
  • 64
  • 83
0

Just add an extra '>' or '<':

uint value1= 4096 >> 2;
uint value2 = 65536 >> 6;
uint value3 = 64 << 4;

http://www.blackwasp.co.uk/CSharpShiftOperators.aspx

RvdK
  • 19,580
  • 4
  • 64
  • 107
0

Are you asking why these relationships exist? Shifting bits left by 1 bit is equivalent to multiplying by 2. So 512 << 1 = 512 * 2 = 1024. Shifting right by 1 is dividing by 2. Shifting by 2 is multiplying/dividing by 4, by n is 2^n. So 1 << 10 = 1 * 2^10 = 1024. To see why, write the number out in binary: let's take 7 as an example:

7 -> 0000 0111b
7 << 1 -> 0000 1110b = 14
7 << 3 -> 0011 1000b = 56

If you already knew all this, I apologize, but you might want to make the question less vague.

mtrw
  • 34,200
  • 7
  • 63
  • 71