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++
// 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 equivalentint 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 bitint 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 codeint main(){ int n = 56, k = 2; cout << getNum(n, k); return 0;} |
Java
// Java implementation of the approachclass 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 |
Python
# Python implementation of the approachimport 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 equivalentdef 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 resultdef 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 n # 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 coden = 56k = 2print(getNum(n, k))# This code is contributed by Sanjit_Prasad |
C#
// C# implementation of the approachusing 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 |
Javascript
<script> // javascript implementation of the approach // Function to convert decimal number n // to its binary representation // stored as an array arr[] function decBinary(arr, n) { let k = parseInt(Math.log2(n), 10); while (n > 0) { arr[k--] = n % 2; n = parseInt(n/2,10); } } // Function to convert the number // represented as a binary array // arr[] into its decimal equivalent function binaryDec(arr, n) { let ans = 0; for (let i = 0; i < n; i++) ans += arr[i] << (n - i - 1); return ans; } // Function to return the updated integer // after flipping the kth bit function getNum(n,k) { // Number of bits in n let l = parseInt(Math.log2(n),10) + 1; // Find the binary // representation of n let a = new Array(l); a.fill(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); } let n = 56, k = 2; document.write(getNum(n, k)); // This code is contributed by vaibhavrabadiya117.</script> |
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


