Binary digits: Difference between revisions
Content deleted Content added
Line 1,588: | Line 1,588: | ||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
{{Incorrect|Oberon-2|There shouldn't be leading zeros in the result. Non-negative integers only for input.}} |
|||
{{Works with|oo2c Version 2}} |
|||
<lang oberon2> |
<lang oberon2> |
||
MODULE BinaryDigits; |
MODULE BinaryDigits; |
||
IMPORT |
IMPORT Out; |
||
Object, |
|||
⚫ | |||
SYSTEM, |
|||
Out; |
|||
PROCEDURE Reverse(VAR str: ARRAY OF CHAR); |
|||
VAR |
|||
s,e: LONGINT; |
|||
c: CHAR; |
|||
BEGIN |
BEGIN |
||
IF x > 1 THEN OutBin(x DIV 2) END; |
|||
Out.Int(x MOD 2, 1); |
|||
WHILE (e >= 0) & (str[e] = 0X) DO DEC(e) END; |
|||
END OutBin; |
|||
WHILE (s < e) DO |
|||
c := str[s]; |
|||
str[s] := str[e]; |
|||
str[e] := c; |
|||
INC(s);DEC(e) |
|||
END |
|||
END Reverse; |
|||
⚫ | |||
VAR |
|||
i: LONGINT; |
|||
o: ARRAY 12 OF CHAR; |
|||
BEGIN |
|||
o[LEN(o) - 1] := 0X; |
|||
i := 0; |
|||
WHILE (i < LEN(o) - 1) DO |
|||
o[i] := CHR(ORD('0') + (x MOD 8)); |
|||
INC(i);x := SYSTEM.LSH(x,-3) |
|||
END; |
|||
Reverse(o); |
|||
RETURN Object.NewLatin1(o) |
|||
END IntToOct; |
|||
PROCEDURE IntToHex*(x: LONGINT):STRING; |
|||
VAR |
|||
i: LONGINT; |
|||
h: ARRAY 9 OF CHAR; |
|||
hexDigit: LONGINT; |
|||
BEGIN |
|||
h[LEN(h) - 1] := 0X; |
|||
i := 0; |
|||
WHILE (i < LEN(h) - 1) DO |
|||
hexDigit := x MOD 16; |
|||
IF (hexDigit >= 0) & (hexDigit <= 9) THEN |
|||
h[i] := CHR(ORD('0') + hexDigit); |
|||
ELSE |
|||
h[i] := CHR(ORD('A') + (hexDigit - 10)); |
|||
END; |
|||
INC(i);x := SYSTEM.LSH(x,-4) |
|||
END; |
|||
Reverse(h); |
|||
RETURN Object.NewLatin1(h) |
|||
END IntToHex; |
|||
PROCEDURE IntToBin*(x: LONGINT):STRING; |
|||
VAR |
|||
i: LONGINT; |
|||
b: ARRAY 33 OF CHAR; |
|||
BEGIN |
|||
b[LEN(b) - 1] := 0X; |
|||
i := 0; |
|||
WHILE (i < LEN(b) - 1) DO |
|||
b[i] := CHR(ORD('0') + (x MOD 2)); |
|||
INC(i);x := SYSTEM.LSH(x,-1) |
|||
END; |
|||
Reverse(b); |
|||
RETURN Object.NewLatin1(b); |
|||
END IntToBin; |
|||
BEGIN |
BEGIN |
||
OutBin(0); Out.Ln; |
|||
OutBin(1); Out.Ln; |
|||
OutBin(2); Out.Ln; |
|||
OutBin(3); Out.Ln; |
|||
Out.Object("MAX(LONGINT) :> " + IntToBin(MAX(LONGINT)));Out.Ln; |
|||
OutBin(42); Out.Ln; |
|||
Out.Object("MIN(LONGINT) :> " + IntToBin(MIN(LONGINT)));Out.Ln; |
|||
END BinaryDigits. |
END BinaryDigits. |
||
</lang> |
</lang> |
||
Line 1,680: | Line 1,610: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
0 |
|||
12 :> 00000000000000000000000000001100 |
|||
1 |
|||
-12 :> 11111111111111111111111111110100 |
|||
10 |
|||
MAX(LONGINT) :> 01111111111111111111111111111111 |
|||
11 |
|||
MIN(LONGINT) :> 10000000000000000000000000000000 |
|||
101010 |
|||
</pre> |
</pre> |
||