By downloading this code you agree to the
Source Code Use License (PDF). |
{version = 1.01; (* of tobinary.p 1998 January 2}
(* begin module describe.tobinary *)
(*
name
tobinary: convert numbers to binary
synopsis
tobinary(input: in, output: out)
files
input: a list of integers, one per line.
If the first number is zero, then that number
is ignored and the decimal numbers are not reported.
output: the numbers and the numbers converted to binary
description
This program makes conversion to binary easy.
The method the program uses is to find out if the number is divisible by
2. This is done with the standard mod function If it is divisible by 2,
then the last digit is 0, otherwise it is 1. Then the number is divided
by 2 and the remainder (1 or 0) is thrown away. This is done with the
standard div function. This process is repeated until the number is
completely eaten away. This gives the digits in reversed order, so the
program stores them in an array, and prints the array backwards to give
you the final result.
examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
32
64
1024
2046
0
0
-1
-2
-3
-4
-5
-6
-7
-8
-9
0
0
1998
1999
2000
2001
2002
0
0
2045
2046
2047
2048
2049
produces:
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
16 10000
17 10001
32 100000
64 1000000
1024 10000000000
2046 11111111110
0 0
0 0
-1 -1
-2 -10
-3 -11
-4 -100
-5 -101
-6 -110
-7 -111
-8 -1000
-9 -1001
0 0
0 0
1998 11111001110
1999 11111001111
2000 11111010000
2001 11111010001
2002 11111010010
0 0
0 0
2045 11111111101
2046 11111111110
2047 11111111111
2048 100000000000
2049 100000000001
documentation
see also
tobinaryp
author
Thomas Dana Schneider
bugs
technical notes
*)
(* end module describe.tobinary *)
{This manual page was created by makman 1.45}
{created by htmlink 1.62}