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);
}
}
}
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