package main
import (
"fmt"
"math"
)
func main(){
binStr := subtractionOfPowers(1234321)
fmt.Println(binStr)
binStr = div2(1234321)
fmt.Println(binStr)
}
func subtractionOfPowers(n int) string{
result := ""
maxN := 0
for i:=0;;i++{
if n < int(math.Pow(2.0, float64(i))) {
maxN = i
break
}
}
if maxN>1{
result = ""
}
rem:=n
for j:=maxN-1; j>0;j--{
temp := rem - int(math.Pow(2.0, float64(j)))
//fmt.Println(rem, int(math.Pow(2.0, float64(j))), temp)
if rem>=int(math.Pow(2.0, float64(j))){
result = result + "1"
rem = temp
} else {
result = result + "0"
}
//fmt.Printf("maxN=%s
", result)
}
if n%2 == 0{
result += "0"
} else {
result += "1"
}
return result
}
func div2(x int) string{
result := ""
n := x
for {
if n%2 == 0 {
result = "0" + result
} else {
result = "1" + result
}
n = n/2
if n <= 0 {
break
}
}
return result
}