bitwiseCalc 1.5.0 → 1.6.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 +140 -181
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5fda77d07643c6b2454ecb235fcf345aa95e5ae0
4
- data.tar.gz: eca2f46eeb79c3a80c65c8e0a4c73e97838337be
3
+ metadata.gz: 291ba8693f8476a508712b9ca6b7fa690435b8ec
4
+ data.tar.gz: 4679cc129d07100ab9bac21999c8ae59bdb07406
5
5
  SHA512:
6
- metadata.gz: 3d3ef0aa78d70027b726d470d887a656d4a9fd719be088ace02845c35fc0291f6386ac4aafd05a260782d644e4692f1e9c1fde2bd4ea2ae300462b5bbb2638d3
7
- data.tar.gz: 4e412b353af419aa280addbe63bc42a79a74163b57d43c5f53fff3b23da718de24312553fa5301ca036eb4a1699c8d49548bd1f063421810f93c6e49410b3cef
6
+ metadata.gz: 1ef57bd68037118cc9642ee42a6caf5e52b65f578d2a5c5cfc2a9c0ddee8e34c92266ac2c86a6f0b87e216401f0d62693375ccb09de2ec03c95228cb62e5e9d9
7
+ data.tar.gz: d42b86871d3ee653c38dfcbee311b19e6e30edd50c104b3e5ea8f1f04a6ed94f4412d655acde58bbca3f100fcc6e2ac8bf0f541317ba62c4c49ea91867afdbdb
data/lib/bitwiseCalc CHANGED
@@ -3,258 +3,220 @@
3
3
  # @Name: Vargas, Hector
4
4
  # @Email: vargash1@wit.edu
5
5
  # @Date: 2015-02-23 10:23:20
6
- # @Last Modified by: Hector Vargas
7
- # @Last Modified time: 2016-01-09 19:20:07
6
+ # @Last modified by: vargash1
7
+ # @Last modified time: Tuesday, May 10th 2016, 5:19:18 pm
8
8
  require 'colorize'
9
+
9
10
  class BoolCalc
10
11
  def initialize()
11
12
  @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']
13
+
14
+ # Valid Operations
15
+ @ops = { :NOT => "Bitwise Not", :SHR => "Shift Right",
16
+ :SHL => "Shift Left", :AND => "Logical And",
17
+ :OR => "Logical Or", :XOR => "Exclusive Or",
18
+ :RAD => "Display Radixes", :ROL => "Circular Rotate Left",
19
+ :ROR => "Circular Rotate Right", :MUL => "Multiply",
20
+ :DIV => " Division", :ADD => "Addition",
21
+ :SUB => "Subtraction", :MOD => "Modulus",
22
+ :POW => "Exponentiation", :NOR => "Negated OR",
23
+ :NAND => "Negated AND", :XNOR => "Complement XOR"}
24
+
16
25
  # Option Hash
17
- @options = {nocolor:false, grouped: false, bit4: true, bit64: true}
26
+ @options = { nocolor: false, grouped: false, bit4: true,
27
+ bit64: true, ipnum: false, nargs: false }
18
28
  end
29
+
19
30
  # Outputs num in radixes mentioned in program description.
20
31
  def print_all_radix(num)
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"
26
- print "Base 16:\t".yellow; print "#{num.to_s(16)}\n"
27
- print "Base 8: \t".yellow; print "#{num.to_s(8)}\n"
32
+ base10 = num.to_s(10)
33
+ base10 = base10.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse! if @options[:grouped]
28
34
  print "Base 2:\n".yellow
29
35
  print_nice_binary(num)
36
+ puts "Base 8: ".yellow; puts "\t#{num.to_s(8)}"
37
+ puts "Base 10:".yellow; puts "\t#{base10}"
38
+ puts "Base 16:".yellow; puts "\t#{num.to_s(16)}"
30
39
  end
40
+
31
41
  # Executes desired operation.
32
42
  def execute()
33
- case
34
- when(@operation == 'RAD')
43
+ case @operation
44
+ when 'RAD'
35
45
  return
36
- when (@operation == 'AND')
46
+ when 'AND'
37
47
  @ans = @x & @y
38
- when (@operation == 'NAND')
39
- @ans = ~(@x & @y)
40
- when (@operation == 'OR')
48
+ when 'NAND'
49
+ @ans = ~(@x & @y)
50
+ when 'OR'
41
51
  @ans = @x | @y
42
- when (@operation == 'NOR')
52
+ when 'NOR'
43
53
  @ans = ~(@x | @y)
44
- when (@operation == 'XOR')
45
- @ans = @x ^ @y
46
- when (@operation == 'XNOR')
54
+ when 'XOR'
55
+ @ans = @x ^ @y
56
+ when 'XNOR'
47
57
  @ans = ~(@x ^ @y)
48
- when (@operation == 'NOT')
58
+ when 'NOT'
49
59
  @ans = ~@x
50
- when (@operation == 'SHR')
60
+ when 'SHR'
51
61
  @ans = @x >> @y
52
- when (@operation == 'SHL')
62
+ when 'SHL'
53
63
  @ans = @x << @y
54
- when (@operation == 'ROL')
64
+ when 'ROL'
55
65
  binarr = int_to_binarr(@x)
56
- binarr = rol(binarr)
57
- @ans = binarr.join.to_i(2)
58
- when(@operation == 'ROR')
66
+ @ans = rol(binarr).join.to_i(2)
67
+ when 'ROR'
59
68
  binarr = int_to_binarr(@x)
60
- binarr = ror(binarr)
61
- @ans = (binarr.join).to_i(2)
62
- when(@operation == 'MUL')
69
+ @ans = ror(binarr).join.to_i(2)
70
+ when 'MUL'
63
71
  @ans = @x * @y
64
- when(@operation == 'ADD')
72
+ when 'ADD'
65
73
  @ans = @x + @y
66
- when(@operation == 'DIV')
74
+ when 'DIV'
67
75
  @ans = @x / @y
68
- when(@operation == 'SUB')
76
+ when 'SUB'
69
77
  @ans = @x - @y
70
- when(@operation == 'MOD')
78
+ when 'MOD'
71
79
  @ans = @x % @y
72
- when(@operation == 'POW')
80
+ when 'POW'
73
81
  @ans = @x ** @y
74
82
  end
75
83
  end
84
+
76
85
  # Neatly displays results
77
86
  def print_results()
78
- String.disable_colorization = true if (@options[:nocolor])
79
-
80
- print "\t---------------------------------------\n".green
87
+ String.disable_colorization = true if @options[:nocolor]
88
+ separator = "\t---------------------------------------\n"
89
+ print separator.green
81
90
  print_all_radix(@x)
82
- if (@y != nil && @operation != "NOT")
83
- puts "\t---------------------------------------\n".green
91
+
92
+ # print 2 passed num only if
93
+ if @y != nil && @operation != "NOT"
94
+ puts separator.green
84
95
  print_all_radix(@y)
85
96
  end
97
+
86
98
  # rad simply outputs num(s) in multiple radixes
87
- if (@operation != "RAD")
99
+ if @operation != "RAD"
88
100
  puts "\n\t-------------#{@operation} Result-----------------".green
89
101
  print_all_radix(@ans)
90
- end
102
+ end
91
103
  end
92
-
104
+
93
105
  # Detect any errors before operation execution
94
106
  def check_for_errors()
95
- # no op read
96
- abort("ERROR, Expected Operation".red) if (@operation.nil?)
97
-
98
- # make sure binary ops have binary input
99
- restriction = ((@operation != "NOT" && @operation != "RAD") &&@y.nil?)
107
+ # no operation read
108
+ abort("ERROR, Expected Operation".red) if @operation.nil?
109
+
110
+ # make sure binary operations have binary input
111
+ restriction = @operation != "NOT" && @operation != "RAD" && @y.nil?
100
112
  # also make sure we have read at least 1 number
101
- if restriction || @x.nil?
113
+ if restriction || @x.nil?
102
114
  abort("ERROR, Expected Number(s) for Operation:\t#{@operation}".red)
103
115
  end
104
-
116
+
105
117
  # div by zero
106
- if ((@operation == "DIV" || @operation == "MOD") && (@y == 0))
118
+ if (@operation == "DIV" || @operation == "MOD") && @y == 0
107
119
  abort("ERROR, Divide by 0 for Operation:\t#{@operation}".red)
108
- end
109
- end
110
-
111
- # Use regular expressions to parse arguments passed.
120
+ end
121
+ end
122
+
123
+ # Parse arguments passed.
112
124
  def parse_args()
113
125
  #exit if no args passed; parse flags only if needed
114
- usage() if ARGV.empty?
115
- parse_flags if (ARGV[0][0] == "-")
116
-
117
- ARGV.each do |a|
118
- case
126
+ usage if ARGV.empty?
127
+ parse_flags if ARGV[0][0] == "-"
128
+
129
+ ARGV.each do |arg|
130
+
131
+ case arg
119
132
  #check if valid operation choice
120
- when (a == a[/[a-zA-Z]*/])
121
- @ops.each do |ops_elem|
122
- if (a.upcase == ops_elem)
123
- @operation = ops_elem
124
- end
125
- end
126
- #hex val was read
127
- when(a == a[/0[xX][0-9a-fA-F]+/])
128
- if (@x == nil) ? @x = a.to_i(16): @y = a.to_i(16)
129
- end
130
- #binary val was read
131
- when(a == a[/0[bB][0-1]+/])
132
- if (@x == nil) ? @x = a.to_i(2): @y = a.to_i(2)
133
- end
134
- #octal val was read
135
- when(a == a[/0[0-7]+/])
136
- if (@x == nil) ? @x = a.to_i(8): @y = a.to_i(8)
137
- end
138
- #decimal val was read
139
- when (is_number(a))
140
- if (@x == nil) ? @x = a.to_i: @y = a.to_i
141
- end
133
+ when arg[/[a-zA-Z]+/]
134
+ @operation = arg.upcase if @ops.has_key?(arg.upcase.to_sym)
135
+ #hex number
136
+ when arg[/0[xX][0-9a-fA-F]+/]
137
+ @x == nil ? @x = arg.to_i(16) : @y = arg.to_i(16)
138
+ #binary number
139
+ when arg[/0[bB][0-1]+/]
140
+ @x == nil ? @x = arg.to_i(2) : @y = arg.to_i(2)
141
+ #octal number
142
+ when arg[/0[0-7]+/]
143
+ @x == nil ? @x = arg.to_i(8) : @y = arg.to_i(8)
144
+ #decimal number
145
+ when arg[/[0-9]+/]
146
+ @x == nil ? @x = arg.to_i : @y = arg.to_i
142
147
  end
143
148
  end
144
149
  end
145
150
  # Parses flags, only called if flags passed are detected.
146
151
  def parse_flags()
147
152
  flags = ARGV[0]
148
- flags.split("").each do |f|
149
- case
150
- when f == "n"
153
+ flags.split("").each do |flag|
154
+ case flag
155
+ when "n"
151
156
  @options[:nocolor] = true
152
- when f == "h"
157
+ when "h"
153
158
  usage
154
- when f == "f"
159
+ when "f"
155
160
  @options[:bit4] = false
156
- when f == "s"
161
+ when "s"
157
162
  @options[:bit64] = false
158
- when f == "g"
163
+ when "g"
159
164
  @options[:grouped] = true
160
165
  end
161
166
  end
162
167
  end
163
- # Simple way to check if our number is truly a number
164
- def is_number(num)
165
- is_num = ((num.to_f.to_s == num.to_s) || (num.to_i.to_s == num.to_s))
166
- return is_num
167
- end
168
+
168
169
  def usage()
169
170
  puts """
170
171
  Description:
171
172
  A simple bitwise calculator that executes the operations noted below.
172
173
  It can accept Octal, Hexadecimal, Decimal, and Binary inputs.
173
- It will output in above mentioned radixes, binary output
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
-
174
+ It will output in above mentioned radixes.
175
+ Binary output will have 32, 64, 128, and up to 256 bit format.
176
+ The results are printed to the console.
178
177
  Usage:
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>
178
+ Pass arguments to the program as shown below. Note that binary output
179
+ is spaced every 4 bits. Every 64 bits, a newline is added for readability.
180
+ For additional help/examples:
181
+ https://github.com/vargash1/Bitwise-Calculator
187
182
 
188
183
  Unary Operations:
189
- bitwiseCalc <flag> <num> <operation>
190
-
184
+ bitwiseCalc [flag] <number> <operation>
185
+
191
186
  Binary Operations:
192
- bitwiseCalc <flag> <num> <operation> <num>
193
-
187
+ bitwiseCalc [flag] <number> <operation> <number>
188
+
194
189
  Passing a number in a radix other than base10:
195
- <base_2_num> = 0b[0-1]
196
- <base_8_num> = 0[0-7]
197
- <base_16_num> = 0x[0-9a-f]
190
+ <base_2_number> = 0b[0-1]
191
+ <base_8_number> = 0[0-7]
192
+ <base_16_number> = 0x[0-9a-f]
198
193
  Flags:
199
194
  -f
200
- disables spacing every 4 bits in binary output
195
+ disables spacing every 4 bits in binary output(enabled by default)
201
196
  -s
202
- disables newline every 64 bits in binary output
197
+ disables newline every 64 bits in binary output(enabled by default)
203
198
  -n
204
- disables colorized output
199
+ disables colorized output(enabled by default)
205
200
  -h
206
- outputs usage and exits
207
- -g
208
- outputs base 10 numbers in grouped form
209
- Operations:
210
- NOT
211
- Bitwise NOT, or complement of input.
212
- RAD
213
- Diplays Input(s) in metioned radixes.
214
- AND
215
- Logical AND.
216
- NAND
217
- Negative AND.
218
- OR
219
- Logical inclusive OR.
220
- NOR
221
- Negative OR.
222
- XOR
223
- Logical exclusive OR.
224
- XNOR
225
- Logical complement of XOR.
226
- SHR
227
- Right logical shift.
228
- SHL
229
- Left logical shift.
230
- ROR
231
- Right circular rotate.
232
- ROL
233
- Left circular rotate.
234
- MUL
235
- Multiplication.
236
- DIV
237
- Division.
238
- ADD
239
- Addition.
240
- SUB
241
- Subtraction.
242
- MOD
243
- Modulus.
244
- POW
245
- Exponentiation.
246
- """
201
+ displays this message and exits
202
+ -g
203
+ outputs base 10 numbers in grouped form(with commas)
204
+ Operations:"""
205
+ @ops.each_pair do |key,val|
206
+ puts "\t #{key}\n\t\t#{val}"
207
+ end
247
208
  exit
248
209
  end
210
+
249
211
  # Outputs number in Binary form for easier reading
212
+ # Will print in increments of 32,64,128, and then 256
250
213
  # Will print 64 bits before moving to the next line
251
214
  def print_nice_binary(num)
252
- case
253
- when num <= 2 ** 31
215
+ if num <= 2 ** 31
254
216
  max = 31
255
- when num <= 2 ** 63
217
+ elsif num <= 2 ** 63
256
218
  max = 63
257
- when num <= 2 ** 127
219
+ elsif num <= 2 ** 127
258
220
  max = 127
259
221
  else
260
222
  max = 255
@@ -262,14 +224,13 @@ class BoolCalc
262
224
  puts "\t#{max+1} Bit Format(MSB First)".yellow
263
225
  print "\t"
264
226
  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 " "
227
+ if (n+1)%64 == 0 && @options[:bit64]
228
+ print "\n\t"
229
+ elsif (n+1)%4 == 0 && @options[:bit4]
230
+ print " "
270
231
  end
271
232
  print num[n]
272
- end
233
+ end
273
234
  puts
274
235
  end
275
236
  #Converts an integer to a binary array
@@ -280,11 +241,11 @@ class BoolCalc
280
241
  end
281
242
  tmparr
282
243
  end
283
- # Rotate right implementation
244
+ # Rotate right implementation
284
245
  def ror(tmparr)
285
246
  arr_tail = tmparr.length-2
286
247
  for i in 1..@y
287
- #save least significant bit(end of array)
248
+ #save least significant bit(end of array)
288
249
  lsb = tmparr[-1]
289
250
  for j in 0..arr_tail
290
251
  if j == 0
@@ -294,15 +255,14 @@ class BoolCalc
294
255
  #save next element, make next element previously saved elem
295
256
  tmp2 = tmparr[j+1]
296
257
  tmparr[j+1] = tmp1
297
- tmp1 = tmp2
258
+ tmp1 = tmp2
298
259
  end
299
260
  #assign least significant bit(end of array) to head of array
300
261
  tmparr[0] = lsb
301
262
  end
302
- print tmparr.length
303
263
  tmparr
304
264
  end
305
- # Rotate left implementation
265
+ # Rotate left implementation
306
266
  def rol(tmparr)
307
267
  arr_tail = tmparr.length-2
308
268
  for i in 1..@y
@@ -315,8 +275,8 @@ class BoolCalc
315
275
  #assign most significant bit to tail
316
276
  tmparr[-1] = msb
317
277
  end
318
- tmparr
319
- end
278
+ tmparr
279
+ end
320
280
  end
321
281
  def main()
322
282
  runtime = BoolCalc.new()
@@ -325,5 +285,4 @@ def main()
325
285
  runtime.execute()
326
286
  runtime.print_results()
327
287
  end
328
- main()
329
-
288
+ main()
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.5.0
4
+ version: 1.6.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: 2016-01-09 00:00:00.000000000 Z
11
+ date: 2016-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  version: '0'
59
59
  requirements: []
60
60
  rubyforge_project:
61
- rubygems_version: 2.2.5
61
+ rubygems_version: 2.6.2
62
62
  signing_key:
63
63
  specification_version: 4
64
64
  summary: BitWise Calculator CLI tool