bitwiseCalc 1.4.11 → 1.5.0

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/bitwiseCalc +104 -79
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d03c669c232793101e0477a7f62b964d82a441a
4
- data.tar.gz: b4185ed96cf87cc044ed3486bc561ea1d80d5009
3
+ metadata.gz: 5fda77d07643c6b2454ecb235fcf345aa95e5ae0
4
+ data.tar.gz: eca2f46eeb79c3a80c65c8e0a4c73e97838337be
5
5
  SHA512:
6
- metadata.gz: 22dccc77bd2f55efadd1f4b9b61e57ffe82bfa60939b62a47938442282477a8e35ab1cdf09557b6a7b71f519249a3c5fd81f5fcc0a24057828ba7c67dcd3c155
7
- data.tar.gz: 371cfa6219ce7dfaa03c7e9b5bbacccfe5b1105c86bdcc6e20d66fd45d804957be85c9ae19033c4b8cf349b265b1f87711939312e70536efd995a1c69124371b
6
+ metadata.gz: 3d3ef0aa78d70027b726d470d887a656d4a9fd719be088ace02845c35fc0291f6386ac4aafd05a260782d644e4692f1e9c1fde2bd4ea2ae300462b5bbb2638d3
7
+ data.tar.gz: 4e412b353af419aa280addbe63bc42a79a74163b57d43c5f53fff3b23da718de24312553fa5301ca036eb4a1699c8d49548bd1f063421810f93c6e49410b3cef
data/lib/bitwiseCalc CHANGED
@@ -3,8 +3,8 @@
3
3
  # @Name: Vargas, Hector
4
4
  # @Email: vargash1@wit.edu
5
5
  # @Date: 2015-02-23 10:23:20
6
- # @Last Modified by: vargash1
7
- # @Last Modified time: 2015-09-17 20:41:15
6
+ # @Last Modified by: Hector Vargas
7
+ # @Last Modified time: 2016-01-09 19:20:07
8
8
  require 'colorize'
9
9
  class BoolCalc
10
10
  def initialize()
@@ -13,12 +13,16 @@ class BoolCalc
13
13
  @ops = ['NOT','SHR','SHL','AND','OR','XOR',
14
14
  'RAD','ROL','ROR','MUL','DIV','ADD',
15
15
  'SUB','MOD','POW','NOR','NAND','XNOR']
16
- # Flags
17
- @no_color = false
16
+ # Option Hash
17
+ @options = {nocolor:false, grouped: false, bit4: true, bit64: true}
18
18
  end
19
19
  # Outputs num in radixes mentioned in program description.
20
20
  def print_all_radix(num)
21
- print "Base 10:\t".yellow; print "#{num.to_s(10)}\n"
21
+ num_base10 = num.to_s(10)
22
+ if @options[:grouped]
23
+ num_base10 = num.to_s(10).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
24
+ end
25
+ print "Base 10:\t".yellow; print "#{num_base10}\n"
22
26
  print "Base 16:\t".yellow; print "#{num.to_s(16)}\n"
23
27
  print "Base 8: \t".yellow; print "#{num.to_s(8)}\n"
24
28
  print "Base 2:\n".yellow
@@ -48,13 +52,13 @@ class BoolCalc
48
52
  when (@operation == 'SHL')
49
53
  @ans = @x << @y
50
54
  when (@operation == 'ROL')
51
- int_to_binary_arr()
52
- rol()
53
- @ans = (@bit64_array_x.join).to_i(2)
55
+ binarr = int_to_binarr(@x)
56
+ binarr = rol(binarr)
57
+ @ans = binarr.join.to_i(2)
54
58
  when(@operation == 'ROR')
55
- int_to_binary_arr()
56
- ror()
57
- @ans = (@bit64_array_x.join).to_i(2)
59
+ binarr = int_to_binarr(@x)
60
+ binarr = ror(binarr)
61
+ @ans = (binarr.join).to_i(2)
58
62
  when(@operation == 'MUL')
59
63
  @ans = @x * @y
60
64
  when(@operation == 'ADD')
@@ -71,6 +75,8 @@ class BoolCalc
71
75
  end
72
76
  # Neatly displays results
73
77
  def print_results()
78
+ String.disable_colorization = true if (@options[:nocolor])
79
+
74
80
  print "\t---------------------------------------\n".green
75
81
  print_all_radix(@x)
76
82
  if (@y != nil && @operation != "NOT")
@@ -86,35 +92,31 @@ class BoolCalc
86
92
 
87
93
  # Detect any errors before operation execution
88
94
  def check_for_errors()
89
- # no operation read
95
+ # no op read
90
96
  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
-
97
+
98
+ # make sure binary ops have binary input
99
+ restriction = ((@operation != "NOT" && @operation != "RAD") &&@y.nil?)
100
+ # also make sure we have read at least 1 number
101
+ if restriction || @x.nil?
102
+ abort("ERROR, Expected Number(s) for Operation:\t#{@operation}".red)
103
+ end
104
+
94
105
  # div by zero
95
106
  if ((@operation == "DIV" || @operation == "MOD") && (@y == 0))
96
107
  abort("ERROR, Divide by 0 for Operation:\t#{@operation}".red)
97
108
  end
98
-
99
- # make sure binary operations have binary input
100
- restriction = (@operation != "NOT" && @operation != "RAD")
101
- if restriction && (@y.nil?)
102
- abort("ERROR, Expected Number(s) for Operation:\t#{@operation}".red)
103
- end
104
109
  end
105
110
 
106
111
  # Use regular expressions to parse arguments passed.
107
- def parse_args(arr = nil)
112
+ def parse_args()
108
113
  #exit if no args passed; parse flags only if needed
109
114
  usage() if ARGV.empty?
110
115
  parse_flags if (ARGV[0][0] == "-")
111
116
 
112
- # for using in a script
113
- arr = ARGV if arr.nil?
114
-
115
- arr.each do |a|
117
+ ARGV.each do |a|
116
118
  case
117
- #if ARGV[a] is letters only,check if its a valid operation
119
+ #check if valid operation choice
118
120
  when (a == a[/[a-zA-Z]*/])
119
121
  @ops.each do |ops_elem|
120
122
  if (a.upcase == ops_elem)
@@ -144,10 +146,19 @@ class BoolCalc
144
146
  def parse_flags()
145
147
  flags = ARGV[0]
146
148
  flags.split("").each do |f|
147
- usage() if f.upcase == "H"
148
- @no_color = true if f.upcase == "N"
149
+ case
150
+ when f == "n"
151
+ @options[:nocolor] = true
152
+ when f == "h"
153
+ usage
154
+ when f == "f"
155
+ @options[:bit4] = false
156
+ when f == "s"
157
+ @options[:bit64] = false
158
+ when f == "g"
159
+ @options[:grouped] = true
160
+ end
149
161
  end
150
- String.disable_colorization = true if (@no_color)
151
162
  end
152
163
  # Simple way to check if our number is truly a number
153
164
  def is_number(num)
@@ -160,12 +171,19 @@ class BoolCalc
160
171
  A simple bitwise calculator that executes the operations noted below.
161
172
  It can accept Octal, Hexadecimal, Decimal, and Binary inputs.
162
173
  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.
174
+ will have 32,64,128, and up to 256 bit format. The results are printed to
175
+ STDOUT.
176
+ Note this was intended to be a command line interface tool only.
177
+
165
178
  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.
179
+ Pass arguments to the program as shown below.
180
+ Note that binary output is spaced every 4 bits.
181
+ Every 64 bits, a newline is added for readability.
182
+ Colorized output is also enabled by default.
183
+ Of course you can disable these if you wish.
184
+
185
+ For additional help/screenshots:
186
+ <https://github.com/vargash1/Bitwise-Calculator>
169
187
 
170
188
  Unary Operations:
171
189
  bitwiseCalc <flag> <num> <operation>
@@ -177,11 +195,17 @@ class BoolCalc
177
195
  <base_2_num> = 0b[0-1]
178
196
  <base_8_num> = 0[0-7]
179
197
  <base_16_num> = 0x[0-9a-f]
180
- Flags:
198
+ Flags:
199
+ -f
200
+ disables spacing every 4 bits in binary output
201
+ -s
202
+ disables newline every 64 bits in binary output
181
203
  -n
182
- disables colorized output(enabled by default)
204
+ disables colorized output
183
205
  -h
184
206
  outputs usage and exits
207
+ -g
208
+ outputs base 10 numbers in grouped form
185
209
  Operations:
186
210
  NOT
187
211
  Bitwise NOT, or complement of input.
@@ -223,75 +247,75 @@ class BoolCalc
223
247
  exit
224
248
  end
225
249
  # Outputs number in Binary form for easier reading
226
- # Plus it lets user see what is going on.
250
+ # Will print 64 bits before moving to the next line
227
251
  def print_nice_binary(num)
228
- if num <= (2**31)
229
- puts "\t32 Bit Format(MSB First)".yellow
230
- print "\t"
231
- 31.downto(0) do |n|
232
- print num[n]
233
- if (n%4 == 0); print " " end
234
- end
235
- puts
236
- end
237
- if num <= (2**63)
238
- puts "\n\t64 Bit Format(MSB First)".yellow
239
- print "\t"
240
- 63.downto(0) do |n|
241
- print num[n]
242
- print " " if (n%4 == 0)
243
- end
252
+ case
253
+ when num <= 2 ** 31
254
+ max = 31
255
+ when num <= 2 ** 63
256
+ max = 63
257
+ when num <= 2 ** 127
258
+ max = 127
244
259
  else
245
- puts "\n\t128 Bit Format(MSB First)".yellow
246
- print "\t"
247
- 127.downto(0) do |n|
248
- print"\n\t" if (n == 63)
249
- print num[n]
250
- print " " if (n%4 == 0)
251
- end
260
+ max = 255
252
261
  end
262
+ puts "\t#{max+1} Bit Format(MSB First)".yellow
263
+ print "\t"
264
+ max.downto(0) do |n|
265
+ case
266
+ when ((n+1)%64 == 0) && @options[:bit64]
267
+ print "\n\t"
268
+ when ((n+1)%4 == 0) && @options[:bit4]
269
+ print " "
270
+ end
271
+ print num[n]
272
+ end
253
273
  puts
254
274
  end
255
275
  #Converts an integer to a binary array
256
- def int_to_binary_arr()
257
- @bit64_array_x = []
258
- 63.downto(0) do |n|
259
- @bit64_array_x << @x[n]
276
+ def int_to_binarr(arr)
277
+ tmparr = []
278
+ 255.downto(0) do |n|
279
+ tmparr << arr[n]
260
280
  end
281
+ tmparr
261
282
  end
262
283
  # Rotate right implementation
263
- def ror()
264
- arr_tail = @bit64_array_x.length-2
284
+ def ror(tmparr)
285
+ arr_tail = tmparr.length-2
265
286
  for i in 1..@y
266
287
  #save least significant bit(end of array)
267
- lsb = @bit64_array_x[-1]
288
+ lsb = tmparr[-1]
268
289
  for j in 0..arr_tail
269
290
  if j == 0
270
- tmp1 = @bit64_array_x[j+1]
271
- @bit64_array_x[j+1] = @bit64_array_x[j]
291
+ tmp1 = tmparr[j+1]
292
+ tmparr[j+1] = tmparr[j]
272
293
  end
273
294
  #save next element, make next element previously saved elem
274
- tmp2 = @bit64_array_x[j+1]
275
- @bit64_array_x[j+1] = tmp1
295
+ tmp2 = tmparr[j+1]
296
+ tmparr[j+1] = tmp1
276
297
  tmp1 = tmp2
277
298
  end
278
299
  #assign least significant bit(end of array) to head of array
279
- @bit64_array_x[0] = lsb
300
+ tmparr[0] = lsb
280
301
  end
302
+ print tmparr.length
303
+ tmparr
281
304
  end
282
305
  # Rotate left implementation
283
- def rol()
284
- arr_tail = @bit64_array_x.length-2
306
+ def rol(tmparr)
307
+ arr_tail = tmparr.length-2
285
308
  for i in 1..@y
286
309
  #save most significant bit(beginning of array)
287
- msb = @bit64_array_x[0]
310
+ msb = tmparr[0]
288
311
  for j in 0..arr_tail
289
312
  #assign next element in array to current element in array
290
- @bit64_array_x[j] = @bit64_array_x[j+1]
313
+ tmparr[j] = tmparr[j+1]
291
314
  end
292
315
  #assign most significant bit to tail
293
- @bit64_array_x[-1] = msb
294
- end
316
+ tmparr[-1] = msb
317
+ end
318
+ tmparr
295
319
  end
296
320
  end
297
321
  def main()
@@ -302,3 +326,4 @@ def main()
302
326
  runtime.print_results()
303
327
  end
304
328
  main()
329
+
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.11
4
+ version: 1.5.0
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-17 00:00:00.000000000 Z
11
+ date: 2016-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -30,7 +30,7 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 0.7.7
33
- description: A simple BitWise Calculator that can peform arithmetic
33
+ description: A simple CLI BitWise Calculator that can peform arithmetic
34
34
  email: hjvargas1213@gmail.com
35
35
  executables:
36
36
  - bitwiseCalc
@@ -61,5 +61,5 @@ rubyforge_project:
61
61
  rubygems_version: 2.2.5
62
62
  signing_key:
63
63
  specification_version: 4
64
- summary: BitWise Calculator
64
+ summary: BitWise Calculator CLI tool
65
65
  test_files: []