Thread: undo bitshift

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Question undo bitshift

    Is it possible to find possible parameters for a bitshift operation?

    that is, for
    Code:
    int literal = 0xf0;
    int result = literal << variable;
    can I apply some bitwise operation to find a value for "variable" given a result and literal.

    It's easy enough to do in my head (just count the least significant digits until I get a 1), but what's the best way to do this with code?

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    i dont know how u can do it by using bitwise opeartor but i just want to say that left shift is the same as multiplying "literal" by 2 raised to the power "variable". So u can divide "result" by "literal" and take its square root to get the value of "variable".
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    sounds right but when I try it, it doesn't work:

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main(int argc, char **argv) {
    
    int variable = 3;
    int literal = 0xf0;
    int result = literal << variable;
    int rl = result/literal;
    printf("literal: %x\n",literal);
    printf("result: %x\n",result);
    printf("result/literal %x \n",rl);
    printf("sqrt(result/literal): %x\n",sqrt(rl));
    
    }
    gives me

    Code:
    $./test
    literal: f0
    result: 780
    result/literal 8 
    sqrt(result/literal): 667f3bcd
    have I missed something?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    sqrt is not the right thing to use here, I'm pretty sure. Also, it returns a floating point value, so using %x is definitely wrong (unless you actually want to see what the floating point value looks like as a integer - but that's unlikely).

    I guess you could do:
    Code:
    x = (int)(log(result / literal) / log(2));
    But it's probably quicker to do:
    Code:
    int c = 0;
    int x = result / literal;
    while (x) {  c++; x >>= 1; }
    c is the number of bits literal was shifted.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    ok, so i guess
    Code:
     variable = c-1;
    since we don't want count the last bit from >>= operation, which would be the first 1.

    thanks everyone!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, yes, or use while(x > 1).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by young turk View Post
    sounds right but when I try it, it doesn't work:

    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main(int argc, char **argv) {
    
    int variable = 3;
    int literal = 0xf0;
    int result = literal << variable;
    int rl = result/literal;
    printf("literal: %x\n",literal);
    printf("result: %x\n",result);
    printf("result/literal %x \n",rl);
    printf("sqrt(result/literal): %x\n",sqrt(rl));
    
    }
    gives me

    Code:
    $./test
    literal: f0
    result: 780
    result/literal 8 
    sqrt(result/literal): 667f3bcd
    have I missed something?
    sorry i was wrong with the square root part. But going in the similar manner and taking the log(with base 2) of 'rl' will give the value of 'variable'.
    I have modified your code by taking the above mentioned into consideration and changed various values of 'variable' and the result was satisfactory.
    Code:
    #include <math.h>
    #include <stdio.h>
    
    int main(void) {
    
    int variable = 5;//changed it to 2,3,4 etc.
    int literal = 4;
    int result = literal << variable;
    float rl = (float)result/literal;
    printf("literal: %d\n",literal);
    printf("result: %d\n",result);
    printf("result/literal %f \n",rl);
    printf("variable=sqrt(result/literal): %f\n",log(rl)/log(2));//log(with base 2) of rl. In C log with a base of 10 is given so i changed it accordingly
    getch();
    return 0;
    
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitshift Crypt
    By ratte in forum C++ Programming
    Replies: 11
    Last Post: 01-10-2008, 02:48 PM
  2. Implementing multiple undo's
    By Bajanine in forum Windows Programming
    Replies: 4
    Last Post: 09-20-2007, 11:21 PM
  3. Bitshift on 64-bit integers.
    By maththeorylvr in forum C Programming
    Replies: 16
    Last Post: 03-18-2005, 04:19 PM
  4. Changing font in rich edit, without affecting undo buffer
    By jverkoey in forum Windows Programming
    Replies: 3
    Last Post: 01-17-2004, 08:54 PM
  5. Cut, copy, undo, AAGG
    By Lurker in forum Windows Programming
    Replies: 1
    Last Post: 05-30-2003, 12:31 AM

Tags for this Thread