Shuffle a pack of cards and answer the query | Java 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
class GFG { static void shuffle ( int N , int key ) { int NO_OF_BITS = N ; int reverse_num = 0 , temp ; for ( int i = 0 ; i < NO_OF_BITS ; i ++ ) { temp = ( key & ( 1 << i ) ) ; if ( temp > 0 ) reverse_num |= ( 1 << ( ( NO_OF_BITS - 1 ) - i ) ) ; } System . out . print ( reverse_num ) ; } public static void main ( String [ ] args ) { int N = 3 ; int key = 3 ; shuffle ( N , key ) ; } }