Thread: Change tow bits

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Change tow bits


    Hello there, how I can exchange two bits ?
    For example:
    We have the number 5 (0101) and we need to change two bits (the user need to choose two bits) - lets say that the user chose the second and the third bits, so the output need to be:
    0101--->1001 (The output is 9).

    Tnx for help :)
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,122
    Rep Power
    9398
    Third and fourth. Bits 2 and 3.

    Take the number and bitwise-xor it with (1<<2 | 1<<3). Or alternatively xor it with 1<<2 then again with 1<<3.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by requinix
    Third and fourth. Bits 2 and 3.

    Take the number and bitwise-xor it with (1<<2 | 1<<3). Or alternatively xor it with 1<<2 then again with 1<<3.
    I've done that and it's still not working..

    Code:
    #include <stdio.h>
     void main() {
     int var = 0x0005;int new_var; int i=3,j=2; 
    new_var=(1<<i); new_var=(1<<j);
     printf("The old number was: %d , and the new number is: %d \n",var,new_var);
     getchar(); }
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    You haven't bitwised-xor'ed the number and the (two) new_var.

IMN logo majestic logo threadwatch logo seochat tools logo