June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,9 @@
shared void run() {
void printBinary(Integer integer) =>
print(Integer.format(integer, 2));
printBinary(5);
printBinary(50);
printBinary(9k);
}

View file

@ -1,10 +1,10 @@
import system'routines.
import extensions.
program =
public program =
[
(5,50,9000) forEach(:n)
[
console printLine(n toLiteral base:2).
console printLine(n toLiteral(2)).
].
].

View file

@ -1,3 +1,9 @@
for i in [0, 5, 50, 9000]
println(i, " => ", bin(i))
for n in (0, 5, 50, 9000)
@printf("%6i → %s\n", n, bin(n))
end
# with pad
println("\nwith pad")
for n in (0, 5, 50, 9000)
@printf("%6i → %s\n", n, bin(n, 20))
end

View file

@ -0,0 +1,12 @@
#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
FUNCTION PBMAIN () AS LONG
LOCAL i, d() AS DWORD
REDIM d(2)
ARRAY ASSIGN d() = 5, 50, 9000
FOR i = 0 TO 2
PRINT STR$(d(i)) & ": " & BIN$(d(i)) & " (" & BIN$(d(i), 32) & ")"
NEXT i
END FUNCTION

View file

@ -0,0 +1,7 @@
Red []
foreach number [5 50 9000] [
;; any returns first not false value, used to cut leading zeroes
binstr: form any [find enbase/base to-binary number 2 "1" "0"]
print reduce [ pad/left number 5 binstr ]
]

View file

@ -0,0 +1,38 @@
Option Explicit
Sub Main_Dec2bin()
Dim Nb As Long
Nb = 5
Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin(Nb)
Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin2(Nb)
Nb = 50
Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin(Nb)
Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin2(Nb)
Nb = 9000
Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin(Nb)
Debug.Print "The decimal value " & Nb & " should produce an output of : " & DecToBin2(Nb)
End Sub
Function DecToBin(ByVal Number As Long) As String
Dim strTemp As String
Do While Number > 1
strTemp = Number - 2 * (Number \ 2) & strTemp
Number = Number \ 2
Loop
DecToBin = Number & strTemp
End Function
Function DecToBin2(ByVal Number As Long, Optional Places As Long) As String
If Number > 511 Then
DecToBin2 = "Error : Number is too large ! (Number must be < 511)"
ElseIf Number < -512 Then
DecToBin2 = "Error : Number is too small ! (Number must be > -512)"
Else
If Places = 0 Then
DecToBin2 = WorksheetFunction.Dec2Bin(Number)
Else
DecToBin2 = WorksheetFunction.Dec2Bin(Number, Places)
End If
End If
End Function

View file

@ -0,0 +1,34 @@
Public Function Bin(ByVal l As Long) As String
Dim i As Long
If l Then
If l And &H80000000 Then 'negative number
Bin = "1" & String$(31, "0")
l = l And (Not &H80000000)
For i = 0 To 30
If l And (2& ^ i) Then
Mid$(Bin, Len(Bin) - i) = "1"
End If
Next i
Else 'positive number
Do While l
If l Mod 2 Then
Bin = "1" & Bin
Else
Bin = "0" & Bin
End If
l = l \ 2
Loop
End If
Else
Bin = "0" 'zero
End If
End Function
'testing:
Public Sub Main()
Debug.Print "5: " & Bin(5)
Debug.Print "50: " & Bin(50)
Debug.Print "9000: " & Bin(9000)
End Sub