How to toggle a bit at specific position in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strconv"
)

func getBits(n int) string {
    return strconv.FormatInt(int64(n), 2)
}

func main() {
    n := 365
    pos := 2

    fmt.Println(getBits(n))

    n ^= (1 << pos)

    fmt.Println(getBits(n))
}



/*
run:
     
101101101
101101001
      
*/

 



answered Apr 3 by avibootz
...