The Wayback Machine - https://web.archive.org/web/20240717031611/https://www.geeksforgeeks.org/set-k-th-bit-given-number/
Open In App

Set the K-th bit of a given number

Last Updated : 19 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number n and a value k. From the right, set the kth bit in the binary representation of n. The position of LSB(or last bit) is 0, second last bit is 1 and so on. Also, 0 <= k < x, where x is the number of bits in the binary representation of n.
Examples: 
 

Input : n = 10, k = 2
Output : 14
(10)10 = (1010)2
Now, set the 2nd bit from right.
(14)10 = (1110)2
2nd bit has been set.

Input : n = 15, k = 3
Output : 15
3rd bit of 15 is already set.

 

To set any bit we use bitwise OR | operator. As we already know bitwise OR | operator evaluates each bit of the result to 1 if any of the operand’s corresponding bit is set (1). In-order to set kth bit of a number we need to shift 1 k times to its left and then perform bitwise OR operation with the number and result of left shift performed just before. 
 

                            In general, (1 << k) | n.

 

C++




// C++ implementation to set the kth bit
// of the given number
#include <bits/stdc++.h>
 
using namespace std;
 
// function to set the kth bit
int setKthBit(int n, int k)
{
    // kth bit of n is being set by this operation
    return ((1 << k) | n);
}
 
// Driver program to test above
int main()
{
    int n = 10, k = 2;
    cout << "Kth bit set number = "
         << setKthBit(n, k);
    return 0;
}


Java




// Java implementation to set the kth bit
// of the given number
 
class GFG {
     
// function to set the kth bit
static int setKthBit(int n, int k)
{
    // kth bit of n is being set by this operation
    return ((1 << k) | n);
}
 
// Driver code
public static void main(String arg[])
{
    int n = 10, k = 2;
    System.out.print("Kth bit set number = " +
                             setKthBit(n, k));
}
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python implementation
# to set the kth bit
# of the given number
 
# function to set
# the kth bit
def setKthBit(n,k):
 
    # kth bit of n is being
    # set by this operation
    return ((1 << k) | n)
     
# Driver code
 
n = 10
k = 2
 
print("Kth bit set number = ",
          setKthBit(n, k))
 
# This code is contributed
# by Anant Agarwal.


C#




// C# implementation to set the
// kth bit of the given number
using System;
 
class GFG {
     
// function to set the kth bit
static int setKthBit(int n, int k)
{
    // kth bit of n is being set
    // by this operation
    return ((1 << k) | n);
}
 
// Driver code
public static void Main()
{
    int n = 10, k = 2;
    Console.Write("Kth bit set number = "
                    + setKthBit(n, k));
                    
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal.


PHP




<?php
// PHP implementation to
// set the kth bit of
// the given number
 
// function to set
// the kth bit
function setKthBit($n, $k)
{
    // kth bit of n is being
    // set by this operation
    return ((1 << $k) | $n);
}
 
// Driver Code
$n = 10; $k = 2;
echo "Kth bit set number = ",
           setKthBit($n, $k);
 
// This code is contributed by m_kit
?>


Javascript




<script>
    // Javascript implementation to set the
    // kth bit of the given number
     
    // function to set the kth bit
    function setKthBit(n, k)
    {
        // kth bit of n is being set
        // by this operation
        return ((1 << k) | n);
    }
     
    let n = 10, k = 2;
    document.write("Kth bit set number = " + setKthBit(n, k));
     
    // This code is contributed by rameshtravel07.
</script>


Output: 
 

Kth bit set number = 14

Time Complexity: O(1)

Auxiliary Space: O(1)

 



Similar Reads

Find the index at which bit must be set to maximise distance between next set bit
Given a binary array arr[]. The task is to find the position of any 0 in arr[] such that the distance between two set bits is maximized. Examples Input: arr = [1, 0, 0, 0, 1, 0, 1]Output: 2Explanation: Flip the bit at arr[2] Input: arr = [1, 0, 0, 0]Output: 3 Approach: The problem can be solved by finding the longest distance between adjacent set b
5 min read
Find array elements with rightmost set bit at the position of the rightmost set bit in K
Given an array arr[] consisting of N and an integer K, the task is to print the elements of arr[] whose rightmost set bit is at the same position as the rightmost set bit in K. Examples: Input: arr[] = { 3, 4, 6, 7, 9, 12, 15 }, K = 7Output: { 3, 7, 9, 15 }Explanation:Binary representation of K (= 7) is 0111.Rightmost set bit in 7 is at position 1.
11 min read
Inserting M into N such that m starts at bit j and ends at bit i | Set-2
Given two 32-bit numbers, N and M, and two-bit positions, i and j. Write a method to insert M into N such that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all of M. Assuming index start from 0.Examples: a) N = 1024 (10000000000), M = 19 (10011), i = 2, j = 6 Output : 1100 (10001001100) b) N
10 min read
Count bases which contains a set bit as the Most Significant Bit in the representation of N
Given a positive integer N, the task is to count the number of different bases in which, when N is represented, the most significant bit of N is a found to be a set bit. Examples: Input: N = 6Output: 4Explanation: The number 6 can be represented in 5 different bases, i.e. base 2, base 3, base 4, base 5, base 6. (6)10 in base 2: (110)2(6)10 in base
5 min read
Count of 1-bit and 2-bit characters in the given binary string
Given two special characters, the first character can be represented by one bit which is 0 and the second character can be represented by two bits either 10 or 11. Now given a string represented by several bits. The task is to return the number of characters it represents. Note that the given string is always valid.Examples: Input: str = "11100" Ou
4 min read
Implementation of Bit Stuffing and Bit Destuffing
Bit Stuffing is a process of inserting an extra bit as 0, once the frame sequence encountered 5 consecutive 1's. Given an array, arr[] of size N consisting of 0's and 1's, the task is to return an array after the bit stuffing. Examples: Input: N = 6, arr[] = {1, 1, 1, 1, 1, 1}Output: 1111101Explanation: During the traversal of the array, 5 consecut
15 min read
Find X in range [1, N] of bit size A[i] such that X^2's bit size is not present in Array
Given an array A of N integers, where: Each element of array represents the bit size of an imaginary unsigned integer datatype.If ith imaginary datatype has a size of A[i] bits, then it can store integers from size 0 to 2A[i]-1. The task is to check if there exist some integer X, such that: X is in range 1 to NX can fit in a datatype of bit size A[
8 min read
Minimize bit flips such that each continuous segment of same bit has even length
Given a binary string S of even size that contains only 0s and 1s, the task is to find the minimum flips (that is 0 to 1 or 1 to 0) such that every contiguous subsegment that contains the same bit is of even size. Examples: Input: S = "1110011000 "Output: 3Explanation: Change S[2], S[5] and S[6] to '0'. After that S becomes "1100000000", it can be
8 min read
Inserting m into n such that m starts at bit j and ends at bit i.
We are given two numbers n and m, and two-bit positions, i and j. Insert bits of m into n starting from j to i. We can assume that the bits j through i have enough space to fit all of m. That is, if m = 10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j = 3 and i = 2, because m could not fully f
8 min read
Minimum operations to set given coordinates as 1 by choosing a set bit index and changing whole row to 1
Given a binary matrix arr[][] having N rows and M columns, the task is to calculate the minimum number of operations required to set the value of the coordinate (x, y) as 1 wherein each operation, select any index such that its value is 1 and set all its row elements or columns elements to 1. Example: Input: arr[][] = {{0, 1, 0, 0}, {1, 1, 1, 0}, {
7 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg