-
Notifications
You must be signed in to change notification settings - Fork 10
/
bitlib.txt
79 lines (69 loc) · 2.42 KB
/
bitlib.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@name bitlib
@model models/bull/gates/microcontroller1.mdl
#[ ]#
## E2 Library: BitLib ##
## ##
## Implements 'bitstrings' (binary strings) ##
## along with various methods to manipulate ##
## and read them. For advanced users only! ##
#[ ]#
if( first() ){
function string toStringByte(N){
local Result = toString(N,2)
return "0":repeat(8-Result:length()) + Result
}
###############
##
# String:getBits( StartBit = 1, EndBit = 1 )
# no arguments: returns the entire bitString of String
# one argument: returns a partial bitstring, starting at StartBit and going to the end of String
# two arguments: returns a partial bitstring, starting at StartBit and going to EndBit
#
function string string:getBits(){
local Bits = ""
for( N=1, This:length() ){
Bits = Bits + toStringByte( toByte(This,N) )
}
return Bits
}
function string string:getBits(StartBit){
local Bits = ""
for( N=ceil(StartBit/8), This:length() ){
Bits = Bits + toStringByte( toByte(This,N) )
}
Bits = Bits:sub((StartBit+1)%8-1)
return Bits
}
function string string:getBits(StartBit,EndBit){
local Bits = ""
for( N=ceil(StartBit/8), ceil(EndBit/8) ){
Bits = Bits + toStringByte( toByte(This,N) )
}
local BitStart = (StartBit)%8
local BitEnd = BitStart + EndBit - StartBit
Bits = Bits:sub(BitStart,BitEnd)
return Bits
}
###############
##
# BitString:parseBits()
# Parses BitString into a normal string; adds padding if BitString is partial
#
function string string:parseBits(){
local String = ""
local Test = This:length()%8
if(Test){
This = This + "1":repeat(8-Test) # create padding
}
#print(This)
for( N=0, ceil(This:length()/8) - 1 ){
local Byte = This:sub(1 + N*8, 8 + N*8)
String = String + toChar( Byte:toNumber(2) )
}
return String
}
if(entity():model() == "models/bull/gates/microcontroller1.mdl"){
selfDestruct()
error("This is a library; #include it in something.")
}
}