Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Monday, August 21, 2017

Set kth bit from right


Explanation :

Blitwise OR is used to set bit , here we are shifting 1 to k and then using or operator to set bit.

Code:

import java.util.*;
import java.lang.*;
import java.io.*;
class hackerranksolutionc
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
   int n=ab.nextInt();
   System.out.println((1<<ab.nextInt())|n);
}
}
}

0 Comments:

Post a Comment

Stats