Given two positive integers n and k. The problem is to check whether the bit at position k from the right in the binary representation of n is set (‘1’) or unset (‘0’).
Constraints: 1 <= k <= number of bits in the binary representation of n.
Examples:
Input : n = 10, k = 2
Output : Set
(10)10 = (1010)2
The 2nd bit from the right is set.
Input : n = 21, k = 4
Output : Unset
Approach#1: Following are the steps:
- Calculate new_num = (n >> (k – 1)).
- if (new_num & 1) == 1 then bit is “Set”, else “Unset”.
C++
#include <bits/stdc++.h>
using namespace std;
bool bitAtGivenPosSetOrUnset(unsigned int n,
unsigned int k)
{
int new_num = n >> (k - 1);
return (new_num & 1);
}
int main()
{
unsigned int n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k))
cout << "Set";
else
cout << "Unset";
return 0;
}
|
Java
import java.io.*;
class GFG {
static int bitAtGivenPosSetOrUnset
( int n, int k)
{
int new_num = n >> (k - 1);
return (new_num & 1);
}
public static void main (String[] args)
{
int n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k)==1)
System.out.println("Set");
else
System.out.println("Unset");
}
}
|
Python3
import math
def bitAtGivenPosSetOrUnset( n, k):
new_num = n >> (k - 1)
return (new_num & 1)
n = 10
k = 2
if (bitAtGivenPosSetOrUnset(n, k)):
print("Set")
else:
print("Unset")
|
C#
using System;
class GFG {
static int bitAtGivenPosSetOrUnset(
int n, int k)
{
int new_num = n >> (k - 1);
return (new_num & 1);
}
public static void Main ()
{
int n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k)==1)
Console.Write("Set");
else
Console.Write("Unset");
}
}
|
PHP
<?php
function bitAtGivenPosSetOrUnset($n, $k)
{
$new_num = $n >> ($k - 1);
return ($new_num & 1);
}
$n = 10;
$k = 2;
if (bitAtGivenPosSetOrUnset($n, $k))
echo "Set";
else
echo "Unset";
?>
|
Javascript
<script>
function bitAtGivenPosSetOrUnset
(n, k)
{
let new_num = n >> (k - 1);
return (new_num & 1);
}
let n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k)==1)
document.write("Set");
else
document.write("Unset");
</script>
|
Output:
Set
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach#2: We can use the left shift to solve this problem. Following are the steps:
- New_num = ( 1 << ( k – 1 ) )
- If ( num & New_num ) == 1 then bit is set else unset.
C++
#include <bits/stdc++.h>
using namespace std;
bool bitAtGivenPosSetOrUnset(unsigned int n,
unsigned int k)
{
int New_num = 1 << (k - 1);
return (New_num & n);
}
int main()
{
unsigned int n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k))
cout << "Set";
else
cout << "Unset";
return 0;
}
|
Java
import java.io.*;
class GFG {
static int bitAtGivenPosSetOrUnset
( int n, int k)
{
int New_num = 1 << (k - 1);
return (New_num & n);
}
public static void main (String[] args)
{
int n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k)==k)
System.out.println("Set");
else
System.out.println("Unset");
}
}
|
Python3
import math
def bitAtGivenPosSetOrUnset( n, k):
New_num = 1 << (k - 1)
return (New_num & n)
n = 10
k = 2
if (bitAtGivenPosSetOrUnset(n, k)):
print("Set")
else:
print("Unset")
|
C#
using System;
class GFG {
static int bitAtGivenPosSetOrUnset(
int n, int k)
{
int New_num = 1 << (k - 1);
return (New_num & n);
}
public static void Main ()
{
int n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k) != 0)
Console.Write("Set");
else
Console.Write("Unset");
}
}
|
Javascript
function bitAtGivenPosSetOrUnset
(n, k)
{
let New_num = 1 << (k -1);
return (New_num & n);
}
let n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k))
console.log("Set");
else
console.log('Unset')
|
PHP
<?php
function bitAtGivenPosSetOrUnset($n, $k)
{
$New_num = 1 << ($k - 1);
return ($New_num & $n);
}
$n = 10;
$k = 2;
if (bitAtGivenPosSetOrUnset($n, $k))
echo "Set";
else
echo "Unset";
?>
|
Output:
Set
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach#3: We can use power of 2 to check if the Bitwise AND of ‘(k-1)th power of 2’ and num is 1 or not. If it is 1 , the bit is set else unset.
C++
#include <bits/stdc++.h>
using namespace std;
bool bitAtGivenPosSetOrUnset(int n, int k)
{
int new_num = pow(2,(k -1));
int x=new_num & n;
if(x > 0)
{ return true;
}
else{
return false;
}
}
int main()
{
int n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k))
cout << "Set";
else
cout << "Unset";
return 0;
}
|
Java
import java.lang.Math;
public class Main {
static boolean bitAtGivenPosSetOrUnset(int n, int k)
{
int new_num = (int) Math.pow(2, k - 1);
int x = new_num & n;
if (x > 0) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
int n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k))
System.out.println("Set");
else
System.out.println("Unset");
}
}
|
Python3
def bitAtGivenPosSetOrUnset(n, k):
new_num = pow(2, k-1)
x = new_num & n
if x > 0:
return True
else:
return False
n = 10
k = 2
if bitAtGivenPosSetOrUnset(n, k):
print("Set")
else:
print("Unset")
|
C#
using System;
public class Program
{
public static bool bitAtGivenPosSetOrUnset(int n, int k)
{
int new_num = (int)Math.Pow(2, (k - 1));
int x = new_num & n;
if (x > 0)
{
return true;
}
else
{
return false;
}
}
public static void Main()
{
int n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k))
Console.WriteLine("Set");
else
Console.WriteLine("Unset");
}
}
|
Javascript
function bitAtGivenPosSetOrUnset(n, k) {
const new_num = Math.pow(2, k - 1);
const x = new_num & n;
if (x > 0) {
return true;
} else {
return false;
}
}
const n = 10, k = 2;
if (bitAtGivenPosSetOrUnset(n, k)) {
console.log("Set");
} else {
console.log("Unset");
}
|
Time Complexity: O(1)
Auxiliary Space: O(1)