Invert the Kth most significant bit of N

Given two non-negative integers N and K, the task is to invert the Kth most significant bit of N and print the number obtained after inverting the bit.

Examples:

Input: N = 10, K = 1
Output: 2
The binary representation of 10 is 1010.
After inverting the first bit it becomes 0010
whose decimal equivalent is 2.

Input: N = 56, K = 2
Output: 40

Approach: Find the number of bits in N, if the number of bits is less than K then N itself is the required answer else flip the Kth most significant bit of N and print the number obtained after flipping it.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to convert decimal number n
// to its binary representation
// stored as an array arr[]
void decBinary(int arr[], int n)
{
    int k = log2(n);
    while (n > 0) {
        arr[k--] = n % 2;
        n /= 2;
    }
}
  
// Function to convert the number
// represented as a binary array
// arr[] into its decimal equivalent
int binaryDec(int arr[], int n)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
        ans += arr[i] << (n - i - 1);
    return ans;
}
  
// Function to return the updated integer
// after flipping the kth bit
int getNum(int n, int k)
{
  
    // Number of bits in n
    int l = log2(n) + 1;
  
    // Find the binary
    // representation of n
    int a[l] = { 0 };
    decBinary(a, n);
  
    // The number of bits in n
    // are less than k
    if (k > l)
        return n;
  
    // Flip the kth bit
    a[k - 1] = (a[k - 1] == 0) ? 1 : 0;
  
    // Return the decimal equivalent
    // of the number
    return binaryDec(a, l);
}
  
// Driver code
int main()
{
    int n = 56, k = 2;
  
    cout << getNum(n, k);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG 
{
      
    // Function to convert decimal number n 
    // to its binary representation 
    // stored as an array arr[] 
    static void decBinary(int arr[], int n) 
    
        int k = (int)(Math.log(n) / 
                      Math.log(2)); 
          
        while (n > 0)
        
            arr[k--] = n % 2
            n /= 2
        
    
      
    // Function to convert the number 
    // represented as a binary array 
    // arr[] into its decimal equivalent 
    static int binaryDec(int arr[], int n) 
    
        int ans = 0
        for (int i = 0; i < n; i++) 
            ans += arr[i] << (n - i - 1); 
        return ans; 
    
      
    // Function to return the updated integer 
    // after flipping the kth bit 
    static int getNum(int n, int k) 
    
      
        // Number of bits in n 
        int l = (int)(Math.log(n) / 
                      Math.log(2)) + 1
      
        // Find the binary 
        // representation of n 
        int a[] = new int[l]; 
        decBinary(a, n); 
      
        // The number of bits in n 
        // are less than k 
        if (k > l) 
            return n; 
      
        // Flip the kth bit 
        a[k - 1] = (a[k - 1] == 0) ? 1 : 0
      
        // Return the decimal equivalent 
        // of the number 
        return binaryDec(a, l); 
    
      
    // Driver code 
    public static void main (String[] args)
    
        int n = 56;
        int k = 2
      
        System.out.println(getNum(n, k)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the approach 
import math
  
# Function to convert decimal number n 
# to its binary representation 
# stored as an array arr[] 
def decBinary(arr, n):
    k = int(math.log2(n)) 
    while (n > 0):
        arr[k] = n % 2
        k = k - 1
        n = n//2
  
# Function to convert the number 
# represented as a binary array 
# arr[] its decimal equivalent 
def binaryDec(arr, n):
    ans = 0
    for i in range(0, n):
        ans = ans + (arr[i] << (n - i - 1))
    return ans
  
# Function to concatenate the binary 
# numbers and return the decimal result 
def getNum(n, k): 
  
    # Number of bits in both the numbers 
    l = int(math.log2(n)) + 1
  
    # Convert the bits in both the gers 
    # to the arrays a[] and b[] 
    a = [0 for i in range(0, l)]
  
    decBinary(a, n)
    # The number of bits in n 
    # are less than k 
    if(k > l):
        return
  
    # Flip the kth bit
    if(a[k - 1] == 0):
        a[k - 1] = 1
    else:
        a[k - 1] = 0
  
    # Return the decimal equivalent 
    # of the number 
    return binaryDec(a, l)
  
# Driver code 
n = 56
k = 2
  
print(getNum(n, k))
  
# This code is contributed by Sanjit_Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG 
{
      
    // Function to convert decimal number n 
    // to its binary representation 
    // stored as an array []arr 
    static void decBinary(int []arr, int n) 
    
        int k = (int)(Math.Log(n) / 
                      Math.Log(2)); 
          
        while (n > 0)
        
            arr[k--] = n % 2; 
            n /= 2; 
        
    
      
    // Function to convert the number 
    // represented as a binary array 
    // []arr into its decimal equivalent 
    static int binaryDec(int []arr, int n) 
    
        int ans = 0; 
        for (int i = 0; i < n; i++) 
            ans += arr[i] << (n - i - 1); 
        return ans; 
    
      
    // Function to return the updated integer 
    // after flipping the kth bit 
    static int getNum(int n, int k) 
    
      
        // Number of bits in n 
        int l = (int)(Math.Log(n) / 
                      Math.Log(2)) + 1; 
      
        // Find the binary 
        // representation of n 
        int []a = new int[l]; 
        decBinary(a, n); 
      
        // The number of bits in n 
        // are less than k 
        if (k > l) 
            return n; 
      
        // Flip the kth bit 
        a[k - 1] = (a[k - 1] == 0) ? 1 : 0; 
      
        // Return the decimal equivalent 
        // of the number 
        return binaryDec(a, l); 
    
      
    // Driver code 
    public static void Main(String[] args)
    
        int n = 56;
        int k = 2; 
      
        Console.WriteLine(getNum(n, k)); 
    
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

40

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.