Shuffle a pack of cards and answer the query | C ++ program to find the card at given index after N shuffles ; function to find card at given index ; Answer will be reversal of N bits from MSB ; Calculating the reverse binary representation ; Printing the result ; driver code ; No . of Shuffle Steps ; Key position
#include <bits/stdc++.h> NEW_LINE using namespace std ; void shuffle ( int N , int key ) { unsigned int NO_OF_BITS = N ; unsigned int reverse_num = 0 , temp ; for ( int i = 0 ; i < NO_OF_BITS ; i ++ ) { temp = ( key & ( 1 << i ) ) ; if ( temp ) reverse_num |= ( 1 << ( ( NO_OF_BITS - 1 ) - i ) ) ; } cout << reverse_num ; } int main ( ) { int N = 3 ; unsigned int key = 3 ; shuffle ( N , key ) ; return 0 ; }