Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

switch every right 4 bit with the left 4 bits

//  Question 4
unsigned int switch_pairs(unsigned int n) {
    unsigned int threes = 0x33333333 ; // unsigned int 32 bits = 4 bytes
    unsigned int twelves = 0xCCCCCCCC ; // unsigned int 32 bits = 4 bytes

    // step one

    /*
       10111001
          &
       00110011 = 0x33

                */
    unsigned int res1 = n&threes;

    // make the every two pairs from right shift to left
    res1 <<=2;

    // step two
    /*
    10111001
       &
    11001100 = 0xCC
             */

    // make every pairs from lift shift to right
    unsigned  int res2 = n&twelves;
    res2 >>=2;

    // step three;
    return res1 | res2;

}
 
PREVIOUS NEXT
Tagged: #switch #bit #left #bits
ADD COMMENT
Topic
Name
7+3 =