- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to turn off the k’th bit in a number.
Example
Consider n = 20(00010100), k = 3 The result after turning off the 3rd bit => 00010000 & ^(1<<(3-1)) => 00010000 & ^(1 << 2) => 00010000 => 16s
Approach to solve this problem
Step 1 − Define a method, where n and k would be the arguments, return type is int.
Step 2 − Perform AND operation with n & ^(1<<(k-1)).
Step 3 − Return the obtained number.
Example
package main import ( "fmt" "strconv" ) func TurnOffKthBit(n, k int) int { return n & ^(1 << (k-1)) } func main(){ var n = 20 var k = 3 fmt.Printf("Binary of %d is: %s.\n", n, strconv.FormatInt(int64(n), 2)) newNumber := TurnOffKthBit(n, k) fmt.Printf("After turning off %d rd bit of %d is: %d.\n", k, n, newNumber) fmt.Printf("Binary of %d is: %s.\n", newNumber, strconv.FormatInt(int64(newNumber), 2)) }
Output
Binary of 20 is: 10100. After turning off 3 rd bit of 20 is 16. Binary of 16 is: 10000.
- Related Articles
- Golang Program to turn on the k’th bit in a number.
- Position of the K-th set bit in a number in C++
- Find value of k-th bit in binary representation in C++
- Check whether K-th bit is set or nots in Python
- How to Turn Off the Water Supply to a Toilet?
- How to programmatically turn off and turn on WiFi in Kotlin?
- Golang program to check if k’th bit is set for a given number or not.
- How to Turn Off Form Autocompletion in HTML?
- 8085 Program to Divide a 16-bit number by an 8-bit number
- 8086 program to divide a 16 bit number by an 8 bit number
- Golang Program to find the position of the rightmost set bit
- k-th prime factor of a given number in java
- Program to find Kth bit in n-th binary string using Python
- Golang Program to Count the Number of Digits in a Number
- Program to find the K-th last node of a linked list in Python

Advertisements