Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmHow to turn on a particular bit in a number?

How to turn on a particular bit in a number?

Given a number n and a value k, turn on the k’th bit in n.
Examples: 
 

Input:  n = 4, k = 2
Output: 6

Input:  n = 3, k = 3
Output: 7

Input:  n = 64, k = 4
Output: 72

Input:  n = 64, k = 5
Output: 80

 

The idea is to use bitwise << and | operators. Using expression “(1 << (k – 1))“, we get a number which has all bits unset, except the k’th bit. If we do bitwise | of this expression with n, we get a number which has all bits same as n except the k’th bit which is 1.
Below is the implementation of above idea.
 

C++




// CPP program to turn on a particular bit
#include <iostream>
using namespace std;
 
// Returns a number that has all bits same as n
// except the k'th bit which is made 1
int turnOnK(int n, int k)
{
    // k must be greater than 0
    if (k <= 0)
        return n;
 
    // Do | of n with a number with all
    // unset bits except the k'th bit
    return (n | (1 << (k - 1)));
}
 
// Driver program to test above function
int main()
{
    int n = 4;
    int k = 2;
    cout << turnOnK(n, k);
    return 0;
}


Java




// Java program to turn on a particular
// bit
class GFG {
     
    // Returns a number that has all
    // bits same as n except the k'th
    // bit which is made 1
    static int turnOnK(int n, int k)
    {
         
        // k must be greater than 0
        if (k <= 0)
            return n;
     
        // Do | of n with a number with
        // all unset bits except the
        // k'th bit
        return (n | (1 << (k - 1)));
    }
     
    // Driver program to test above
    // function
    public static void main(String [] args)
    {
        int n = 4;
        int k = 2;
        System.out.print(turnOnK(n, k));
    }
}
 
// This code is contributed by Smitha


Python 3




# Python 3 program to turn on a
# particular bit
 
# Returns a number that has all
# bits same as n except the k'th
# bit which is made 1
def turnOnK(n, k):
 
    # k must be greater than 0
    if (k <= 0):
        return n
 
    # Do | of n with a number
    # with all unset bits except
    # the k'th bit
    return (n | (1 << (k - 1)))
 
# Driver program to test above
# function
n = 4
k = 2
print(turnOnK(n, k))
 
# This code is contributed by
# Smitha


C#




// C# program to turn on a particular
// bit
using System;
 
class GFG {
     
    // Returns a number that has all
    // bits same as n except the k'th
    // bit which is made 1
    static int turnOnK(int n, int k)
    {
         
        // k must be greater than 0
        if (k <= 0)
            return n;
     
        // Do | of n with a number
        // with all unset bits except
        // the k'th bit
        return (n | (1 << (k - 1)));
    }
     
    // Driver program to test above
    // function
    public static void Main()
    {
        int n = 4;
        int k = 2;
        Console.Write(turnOnK(n, k));
    }
}
 
// This code is contributed by Smitha


PHP




<?php
// PHP program to turn on a particular bit
 
// Returns a number that has
// all bits same as n except
// the k'th bit which is made 1
function turnOnK($n, $k)
{
     
    // k must be greater than 0
    if ($k <= 0)
        return $n;
 
    // Do | of n with a number with all
    // unset bits except the k'th bit
    return ($n | (1 << ($k - 1)));
}
 
    // Driver Code
    $n = 4;
    $k = 2;
    echo turnOnK($n, $k);
 
// This code is contributed by m_kit
?>


Javascript




<script>
    // Javascript program to turn on a particular bit
     
    // Returns a number that has all
    // bits same as n except the k'th
    // bit which is made 1
    function turnOnK(n, k)
    {
           
        // k must be greater than 0
        if (k <= 0)
            return n;
       
        // Do | of n with a number
        // with all unset bits except
        // the k'th bit
        return (n | (1 << (k - 1)));
    }
     
    let n = 4;
    let k = 2;
    document.write(turnOnK(n, k));
 
// This code is contributed by suresh07.
</script>


Output: 

6

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments