Programming-Idioms

History of Idiom 76 > diff from v21 to v22

Edit summary for version 22 :

Version 21

2015-08-22, 23:09:51

Version 22

2015-08-22, 23:24:26

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
var Iter,n:integer;
[...]
 S := '';
  for Iter := 0 to n do
    if (x shr Iter) and 1 = 0 then
      S := '0' + S
    else
      S := '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