Programming-Idioms

History of Idiom 76 > diff from v39 to v40

Edit summary for version 40 :
New Csharp implementation by user [javasucks]

Version 39

2016-02-17, 11:21:46

Version 40

2016-02-18, 16:58:01

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

Idiom #76 Binary digits from an integer

Create the string s of integer x written in base 2.

E.g. 13 -> "1101"

Code
String s = Integer.toBinaryString(x)
Code
String s = Integer.toBinaryString(x)
Doc URL
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toBinaryString%28int%29
Doc URL
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toBinaryString%28int%29
Imports
import "fmt"
import "math/big"
Imports
import "fmt"
import "math/big"
Code
s := fmt.Sprintf("%b", x)
Code
s := fmt.Sprintf("%b", x)
Comments bubble
x has type *big.Int.

This works because *big.Int implements the fmt.Formatter interface.
Comments bubble
x has type *big.Int.

This works because *big.Int implements the fmt.Formatter interface.
Doc URL
https://golang.org/pkg/fmt/#pkg-overview
Doc URL
https://golang.org/pkg/fmt/#pkg-overview
Origin
http://stackoverflow.com/a/23317788/871134
Origin
http://stackoverflow.com/a/23317788/871134
Demo URL
http://play.golang.org/p/2Oy1pcgGZU
Demo URL
http://play.golang.org/p/2Oy1pcgGZU
Imports
import "strconv"
Imports
import "strconv"
Code
s := strconv.FormatInt(x, 2)
Code
s := strconv.FormatInt(x, 2)
Comments bubble
Here x has type int64.

For very big numbers, prefer type *big.Int.
Comments bubble
Here x has type int64.

For very big numbers, prefer type *big.Int.
Doc URL
https://golang.org/pkg/strconv/#FormatInt
Doc URL
https://golang.org/pkg/strconv/#FormatInt
Origin
http://stackoverflow.com/a/13870865/871134
Origin
http://stackoverflow.com/a/13870865/871134
Demo URL
http://play.golang.org/p/Puc5oe4Lv5
Demo URL
http://play.golang.org/p/Puc5oe4Lv5