Programming-Idioms

History of Idiom 76 > diff from v28 to v29

Edit summary for version 29 :

Version 28

2015-09-04, 09:09:19

Version 29

2015-10-29, 14:05:16

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
Integer.to_string(x, 2)
Code
Integer.to_string(x, 2)
Doc URL
http://elixir-lang.org/docs/v1.0/elixir/Integer.html#to_string/2
Doc URL
http://elixir-lang.org/docs/v1.0/elixir/Integer.html#to_string/2
Demo URL
http://play.elixirbyexample.com/s/d2e09a39c8
Demo URL
http://play.elixirbyexample.com/s/d2e09a39c8
Code
s = x.to_s(2)
s = "%b" % x
Code
s = x.to_s(2)
s = "%b" % x
Doc URL
http://ruby-doc.org/core-2.2.3/Fixnum.html#method-i-to_s
Doc URL
http://ruby-doc.org/core-2.2.3/Fixnum.html#method-i-to_s
Code
$s = sprintf("%b", $x);
Code
$s = sprintf("%b", $x);
Code
let s = format!("{:b}", x);
Code
let s = format!("{:b}", x);
Comments bubble
Formatting lets you choose another representation (e.g., `b` for binary, `X` for hex in upper case and `_?` for debug output).
Comments bubble
Formatting lets you choose another representation (e.g., `b` for binary, `X` for hex in upper case and `_?` for debug output).
Doc URL
http://doc.rust-lang.org/std/fmt/index.html
Doc URL
http://doc.rust-lang.org/std/fmt/index.html
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%2013%3B%0A%20%20%20%20let%20s%20%3D%20format!(%22%7B%3Ab%7D%22%2C%20x)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20s)%3B%0A%7D&version=stable
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20x%20%3D%2013%3B%0A%20%20%20%20let%20s%20%3D%20format!(%22%7B%3Ab%7D%22%2C%20x)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20s)%3B%0A%7D&version=stable
Code
var Iter,n:integer;
[...]
  S := '';
  for Iter := 0 to n do
    s:= Char(Ord('0')+(x shr Iter) and 1) + S;   
Code
var Iter,n:integer;
[...]
  S := '';
  for Iter := 0 to n do
    s:= Char(Ord('0')+(x shr Iter) and 1) + S;   
Comments bubble
n: Number of digits
Comments bubble
n: Number of digits
Imports
import std.conv;
Imports
import std.conv;
Code
auto s = to!string(13,2);
Code
auto s = to!string(13,2);
Comments bubble
using std.conv.to(int value, base)
Comments bubble
using std.conv.to(int value, base)
Doc URL
http://dlang.org/phobos/std_conv.html#.to
Doc URL
http://dlang.org/phobos/std_conv.html#.to
Demo URL
http://dpaste.dzfl.pl/e384473470a0
Demo URL
http://dpaste.dzfl.pl/e384473470a0
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.
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.
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