bitwiseCalc 1.4.1 → 1.4.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/bitwiseCalc +108 -69
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d03c669c232793101e0477a7f62b964d82a441a
|
4
|
+
data.tar.gz: b4185ed96cf87cc044ed3486bc561ea1d80d5009
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22dccc77bd2f55efadd1f4b9b61e57ffe82bfa60939b62a47938442282477a8e35ab1cdf09557b6a7b71f519249a3c5fd81f5fcc0a24057828ba7c67dcd3c155
|
7
|
+
data.tar.gz: 371cfa6219ce7dfaa03c7e9b5bbacccfe5b1105c86bdcc6e20d66fd45d804957be85c9ae19033c4b8cf349b265b1f87711939312e70536efd995a1c69124371b
|
data/lib/bitwiseCalc
CHANGED
@@ -4,17 +4,19 @@
|
|
4
4
|
# @Email: vargash1@wit.edu
|
5
5
|
# @Date: 2015-02-23 10:23:20
|
6
6
|
# @Last Modified by: vargash1
|
7
|
-
# @Last Modified time: 2015-09-
|
7
|
+
# @Last Modified time: 2015-09-17 20:41:15
|
8
8
|
require 'colorize'
|
9
9
|
class BoolCalc
|
10
10
|
def initialize()
|
11
|
-
@x = nil
|
12
|
-
|
13
|
-
|
11
|
+
@x = nil; @y = nil; @ans = nil; @operation = nil
|
12
|
+
# Valid Operations
|
13
|
+
@ops = ['NOT','SHR','SHL','AND','OR','XOR',
|
14
|
+
'RAD','ROL','ROR','MUL','DIV','ADD',
|
15
|
+
'SUB','MOD','POW','NOR','NAND','XNOR']
|
16
|
+
# Flags
|
14
17
|
@no_color = false
|
15
|
-
@ops = ['NOT','SHR','SHL','AND','OR','XOR','RAD','ROL','ROR','MUL','DIV','ADD','SUB','MOD','POW']
|
16
|
-
|
17
18
|
end
|
19
|
+
# Outputs num in radixes mentioned in program description.
|
18
20
|
def print_all_radix(num)
|
19
21
|
print "Base 10:\t".yellow; print "#{num.to_s(10)}\n"
|
20
22
|
print "Base 16:\t".yellow; print "#{num.to_s(16)}\n"
|
@@ -22,16 +24,23 @@ class BoolCalc
|
|
22
24
|
print "Base 2:\n".yellow
|
23
25
|
print_nice_binary(num)
|
24
26
|
end
|
27
|
+
# Executes desired operation.
|
25
28
|
def execute()
|
26
29
|
case
|
27
30
|
when(@operation == 'RAD')
|
28
31
|
return
|
29
32
|
when (@operation == 'AND')
|
30
33
|
@ans = @x & @y
|
34
|
+
when (@operation == 'NAND')
|
35
|
+
@ans = ~(@x & @y)
|
31
36
|
when (@operation == 'OR')
|
32
37
|
@ans = @x | @y
|
38
|
+
when (@operation == 'NOR')
|
39
|
+
@ans = ~(@x | @y)
|
33
40
|
when (@operation == 'XOR')
|
34
41
|
@ans = @x ^ @y
|
42
|
+
when (@operation == 'XNOR')
|
43
|
+
@ans = ~(@x ^ @y)
|
35
44
|
when (@operation == 'NOT')
|
36
45
|
@ans = ~@x
|
37
46
|
when (@operation == 'SHR')
|
@@ -60,10 +69,11 @@ class BoolCalc
|
|
60
69
|
@ans = @x ** @y
|
61
70
|
end
|
62
71
|
end
|
72
|
+
# Neatly displays results
|
63
73
|
def print_results()
|
64
74
|
print "\t---------------------------------------\n".green
|
65
75
|
print_all_radix(@x)
|
66
|
-
if (@y != nil)
|
76
|
+
if (@y != nil && @operation != "NOT")
|
67
77
|
puts "\t---------------------------------------\n".green
|
68
78
|
print_all_radix(@y)
|
69
79
|
end
|
@@ -73,27 +83,35 @@ class BoolCalc
|
|
73
83
|
print_all_radix(@ans)
|
74
84
|
end
|
75
85
|
end
|
86
|
+
|
87
|
+
# Detect any errors before operation execution
|
76
88
|
def check_for_errors()
|
77
|
-
# no operation
|
78
|
-
abort
|
79
|
-
|
89
|
+
# no operation read
|
90
|
+
abort("ERROR, Expected Operation".red) if (@operation.nil?)
|
91
|
+
# at least one number read to continue
|
92
|
+
abort("ERROR, Expected Number(s) for #{@operation}".red) if (@x.nil?)
|
93
|
+
|
80
94
|
# div by zero
|
81
95
|
if ((@operation == "DIV" || @operation == "MOD") && (@y == 0))
|
82
96
|
abort("ERROR, Divide by 0 for Operation:\t#{@operation}".red)
|
83
97
|
end
|
84
98
|
|
85
|
-
#
|
99
|
+
# make sure binary operations have binary input
|
86
100
|
restriction = (@operation != "NOT" && @operation != "RAD")
|
87
|
-
if restriction && (
|
88
|
-
abort("ERROR,
|
101
|
+
if restriction && (@y.nil?)
|
102
|
+
abort("ERROR, Expected Number(s) for Operation:\t#{@operation}".red)
|
89
103
|
end
|
90
|
-
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# Use regular expressions to parse arguments passed.
|
91
107
|
def parse_args(arr = nil)
|
92
108
|
#exit if no args passed; parse flags only if needed
|
93
109
|
usage() if ARGV.empty?
|
94
|
-
parse_flags if (
|
110
|
+
parse_flags if (ARGV[0][0] == "-")
|
95
111
|
|
112
|
+
# for using in a script
|
96
113
|
arr = ARGV if arr.nil?
|
114
|
+
|
97
115
|
arr.each do |a|
|
98
116
|
case
|
99
117
|
#if ARGV[a] is letters only,check if its a valid operation
|
@@ -122,106 +140,126 @@ class BoolCalc
|
|
122
140
|
end
|
123
141
|
end
|
124
142
|
end
|
143
|
+
# Parses flags, only called if flags passed are detected.
|
125
144
|
def parse_flags()
|
126
145
|
flags = ARGV[0]
|
127
146
|
flags.split("").each do |f|
|
128
|
-
usage() if f == "H"
|
147
|
+
usage() if f.upcase == "H"
|
129
148
|
@no_color = true if f.upcase == "N"
|
130
149
|
end
|
131
150
|
String.disable_colorization = true if (@no_color)
|
132
151
|
end
|
152
|
+
# Simple way to check if our number is truly a number
|
133
153
|
def is_number(num)
|
134
154
|
is_num = ((num.to_f.to_s == num.to_s) || (num.to_i.to_s == num.to_s))
|
135
155
|
return is_num
|
136
156
|
end
|
137
157
|
def usage()
|
138
158
|
puts """
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
159
|
+
Description:
|
160
|
+
A simple bitwise calculator that executes the operations noted below.
|
161
|
+
It can accept Octal, Hexadecimal, Decimal, and Binary inputs.
|
162
|
+
It will output in above mentioned radixes, binary output
|
163
|
+
will have 32,64, and up to 128 bit format. The results are printed to
|
164
|
+
STDOUT.
|
165
|
+
Usage:
|
166
|
+
Any flags passed MUST come first.
|
167
|
+
Numbers, and operation order DOES NOT MATTER.
|
168
|
+
Binary operations WILL be parsed left to right.
|
169
|
+
|
170
|
+
Unary Operations:
|
171
|
+
bitwiseCalc <flag> <num> <operation>
|
172
|
+
|
173
|
+
Binary Operations:
|
174
|
+
bitwiseCalc <flag> <num> <operation> <num>
|
175
|
+
|
176
|
+
Passing a number in a radix other than base10:
|
177
|
+
<base_2_num> = 0b[0-1]
|
178
|
+
<base_8_num> = 0[0-7]
|
179
|
+
<base_16_num> = 0x[0-9a-f]
|
180
|
+
Flags:
|
144
181
|
-n
|
145
182
|
disables colorized output(enabled by default)
|
146
183
|
-h
|
147
184
|
outputs usage and exits
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
"""
|
185
|
+
Operations:
|
186
|
+
NOT
|
187
|
+
Bitwise NOT, or complement of input.
|
188
|
+
RAD
|
189
|
+
Diplays Input(s) in metioned radixes.
|
190
|
+
AND
|
191
|
+
Logical AND.
|
192
|
+
NAND
|
193
|
+
Negative AND.
|
194
|
+
OR
|
195
|
+
Logical inclusive OR.
|
196
|
+
NOR
|
197
|
+
Negative OR.
|
198
|
+
XOR
|
199
|
+
Logical exclusive OR.
|
200
|
+
XNOR
|
201
|
+
Logical complement of XOR.
|
202
|
+
SHR
|
203
|
+
Right logical shift.
|
204
|
+
SHL
|
205
|
+
Left logical shift.
|
206
|
+
ROR
|
207
|
+
Right circular rotate.
|
208
|
+
ROL
|
209
|
+
Left circular rotate.
|
210
|
+
MUL
|
211
|
+
Multiplication.
|
212
|
+
DIV
|
213
|
+
Division.
|
214
|
+
ADD
|
215
|
+
Addition.
|
216
|
+
SUB
|
217
|
+
Subtraction.
|
218
|
+
MOD
|
219
|
+
Modulus.
|
220
|
+
POW
|
221
|
+
Exponentiation.
|
222
|
+
"""
|
187
223
|
exit
|
188
224
|
end
|
189
|
-
#
|
190
|
-
#
|
225
|
+
# Outputs number in Binary form for easier reading
|
226
|
+
# Plus it lets user see what is going on.
|
191
227
|
def print_nice_binary(num)
|
192
228
|
if num <= (2**31)
|
193
|
-
puts "\t32 Bit Format".yellow
|
229
|
+
puts "\t32 Bit Format(MSB First)".yellow
|
194
230
|
print "\t"
|
195
231
|
31.downto(0) do |n|
|
196
232
|
print num[n]
|
197
233
|
if (n%4 == 0); print " " end
|
198
234
|
end
|
235
|
+
puts
|
199
236
|
end
|
200
237
|
if num <= (2**63)
|
201
|
-
puts "\n\t64 Bit Format".yellow
|
238
|
+
puts "\n\t64 Bit Format(MSB First)".yellow
|
202
239
|
print "\t"
|
203
240
|
63.downto(0) do |n|
|
204
241
|
print num[n]
|
205
|
-
if (n%4 == 0)
|
242
|
+
print " " if (n%4 == 0)
|
206
243
|
end
|
207
244
|
else
|
208
|
-
puts "\n\t128 Bit Format".yellow
|
245
|
+
puts "\n\t128 Bit Format(MSB First)".yellow
|
209
246
|
print "\t"
|
210
247
|
127.downto(0) do |n|
|
211
|
-
if (n == 63)
|
248
|
+
print"\n\t" if (n == 63)
|
212
249
|
print num[n]
|
213
|
-
if (n%4 == 0)
|
250
|
+
print " " if (n%4 == 0)
|
214
251
|
end
|
215
252
|
end
|
216
253
|
puts
|
217
254
|
end
|
218
|
-
#
|
255
|
+
#Converts an integer to a binary array
|
219
256
|
def int_to_binary_arr()
|
220
257
|
@bit64_array_x = []
|
221
258
|
63.downto(0) do |n|
|
222
259
|
@bit64_array_x << @x[n]
|
223
260
|
end
|
224
261
|
end
|
262
|
+
# Rotate right implementation
|
225
263
|
def ror()
|
226
264
|
arr_tail = @bit64_array_x.length-2
|
227
265
|
for i in 1..@y
|
@@ -241,6 +279,7 @@ class BoolCalc
|
|
241
279
|
@bit64_array_x[0] = lsb
|
242
280
|
end
|
243
281
|
end
|
282
|
+
# Rotate left implementation
|
244
283
|
def rol()
|
245
284
|
arr_tail = @bit64_array_x.length-2
|
246
285
|
for i in 1..@y
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitwiseCalc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hector Vargas
|
8
8
|
autorequire:
|
9
9
|
bindir: lib
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|