bitwiseCalc 1.3.2 → 1.4.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 +78 -48
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd231233410a0cbaf9895e9af65a59a324fd93ef
4
- data.tar.gz: 8f53ece11b4d2f48e03b1b0ea60137bc5cf8d210
3
+ metadata.gz: b89270a2ed28dddd5d4545572fb64dd23666ede6
4
+ data.tar.gz: 0eabb7b70a17b952e5382f3eb837c1b5e8290990
5
5
  SHA512:
6
- metadata.gz: e1fe010d775788ee75fb03cd817a044d3fe1cd03a30ee6e9d8a08f310a403c883c126274e099aead4de766633fd0ed068b5d0862376fb3b1a890a1d944d71b38
7
- data.tar.gz: 8741f577deb99e3e43afb90eb4a9a837e4714f69c477b1a93e96848bd18a62f045c8fe874aa529c7b83bc143a7370b5f98f5edc8c74bb7367579c06b661d841d
6
+ metadata.gz: dbc0dc7b9bbea0baf005afa70ada2505d64bb8c1d355dc218664f65f84c66d7d4a87f779bca75ea925cc99d2880790d49aa7cd94e1c9616662a7b44834b7cc4b
7
+ data.tar.gz: 618c5e820839fb3005c50cc80ea2b4a1a23b764c87118d9a888d0d95f72014ae7eaa3eaeee2c38f449cbb5367472ee9e44d0ff81aab6894b520fd39be3226fc4
data/lib/bitwiseCalc CHANGED
@@ -4,9 +4,17 @@
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-06-30 12:22:35
7
+ # @Last Modified time: 2015-09-13 13:42:24
8
8
  require 'colorize'
9
9
  class BoolCalc
10
+ def initialize()
11
+ @x = nil ; @y = nil
12
+ @ans = nil; @operation = nil
13
+ # flags
14
+ @no_color = false
15
+ @ops = ['NOT','SHR','SHL','AND','OR','XOR','RAD','ROL','ROR','MUL','DIV','ADD','SUB','MOD','POW']
16
+
17
+ end
10
18
  def print_all_radix(num)
11
19
  print "Base 10:\t".yellow; print "#{num.to_s(10)}\n"
12
20
  print "Base 16:\t".yellow; print "#{num.to_s(16)}\n"
@@ -15,75 +23,82 @@ class BoolCalc
15
23
  print_nice_binary(num)
16
24
  end
17
25
  def execute()
18
- print "\t---------------------------------------\n".green
19
- print_all_radix(@x)
20
- if (@y != nil)
21
- puts "\t---------------------------------------\n".green
22
- print_all_radix(@y)
23
- end
24
26
  case
25
27
  when(@operation == 'RAD')
26
28
  return
27
29
  when (@operation == 'AND')
28
- final_result = @x & @y
30
+ @ans = @x & @y
29
31
  when (@operation == 'OR')
30
- final_result = @x | @y
32
+ @ans = @x | @y
31
33
  when (@operation == 'XOR')
32
- final_result = @x ^ @y
34
+ @ans = @x ^ @y
33
35
  when (@operation == 'NOT')
34
- final_result = ~@x
36
+ @ans = ~@x
35
37
  when (@operation == 'SHR')
36
- final_result = @x >> @y
38
+ @ans = @x >> @y
37
39
  when (@operation == 'SHL')
38
- final_result = @x << @y
40
+ @ans = @x << @y
39
41
  when (@operation == 'ROL')
40
42
  int_to_binary_arr()
41
43
  rol()
42
- final_result = (@bit64_array_x.join).to_i(2)
44
+ @ans = (@bit64_array_x.join).to_i(2)
43
45
  when(@operation == 'ROR')
44
46
  int_to_binary_arr()
45
47
  ror()
46
- final_result = (@bit64_array_x.join).to_i(2)
48
+ @ans = (@bit64_array_x.join).to_i(2)
47
49
  when(@operation == 'MUL')
48
- final_result = @x * @y
50
+ @ans = @x * @y
49
51
  when(@operation == 'ADD')
50
- final_result = @x + @y
52
+ @ans = @x + @y
51
53
  when(@operation == 'DIV')
52
- final_result = @x / @y
54
+ @ans = @x / @y
53
55
  when(@operation == 'SUB')
54
- final_result = @x - @y
56
+ @ans = @x - @y
55
57
  when(@operation == 'MOD')
56
- final_result = @x % @y
58
+ @ans = @x % @y
57
59
  when(@operation == 'POW')
58
- final_result = @x ** @y
60
+ @ans = @x ** @y
59
61
  end
60
- puts "\n\t-------------#{@operation} Result-----------------".green
61
- print_all_radix(final_result)
62
62
  end
63
- def check_for_errors()
64
- if (@operation == nil)
65
- abort ("ERROR, Expected Operation".red)
63
+ def print_results()
64
+ print "\t---------------------------------------\n".green
65
+ print_all_radix(@x)
66
+ if (@y != nil)
67
+ puts "\t---------------------------------------\n".green
68
+ print_all_radix(@y)
66
69
  end
67
- #not operation is unary
68
- #rad can be either unary or binary
69
- #following logic avoids running a binary operation when user entered
70
- #only one number
71
- operation_restriction = (@operation != "NOT" && @operation != "RAD")
72
- if ((operation_restriction && (@y == nil || @y == 0)) || (@x == nil || @x == 0))
70
+ # rad simply outputs num(s) in multiple radixes
71
+ if (@operation != "RAD")
72
+ puts "\n\t-------------#{@operation} Result-----------------".green
73
+ print_all_radix(@ans)
74
+ end
75
+ end
76
+ def check_for_errors()
77
+ # no operation specified
78
+ abort ("ERROR, Expected Operation".red) if (@operation.nil?)
79
+
80
+ # div by zero
81
+ if ((@operation == "DIV" || @operation == "MOD") && (@y == 0))
82
+ abort("ERROR, Divide by 0 for Operation:\t#{@operation}".red)
83
+ end
84
+
85
+ # all other operations require binary input
86
+ restriction = (@operation != "NOT" && @operation != "RAD")
87
+ if restriction && ((@y == nil ) && (@x == nil))
73
88
  abort("ERROR, Missing Numbers for Operation:\t#{@operation}".red)
74
89
  end
75
90
  end
76
- def parse_args()
77
- ops = ['NOT','SHR','SHL','AND','OR','XOR','RAD','ROL','ROR','MUL','DIV','ADD','SUB','MOD','POW']
78
- if (ARGV.length == 0 || (ARGV[0].upcase) == "-H")
79
- print_help()
80
- exit
81
- end
82
- ARGV.each do |a|
91
+ def parse_args(arr = nil)
92
+ #exit if no args passed; parse flags only if needed
93
+ usage() if ARGV.empty?
94
+ parse_flags if (!is_number(ARGV[0]))
95
+
96
+ arr = ARGV if arr.nil?
97
+ arr.each do |a|
83
98
  case
84
99
  #if ARGV[a] is letters only,check if its a valid operation
85
100
  when (a == a[/[a-zA-Z]*/])
86
- ops.each do |ops_elem|
101
+ @ops.each do |ops_elem|
87
102
  if (a.upcase == ops_elem)
88
103
  @operation = ops_elem
89
104
  end
@@ -107,22 +122,36 @@ class BoolCalc
107
122
  end
108
123
  end
109
124
  end
125
+ def parse_flags()
126
+ flags = ARGV[0]
127
+ flags.split("").each do |f|
128
+ usage() if f == "H"
129
+ @no_color = true if f.upcase == "N"
130
+ end
131
+ String.disable_colorization = true if (@no_color)
132
+ end
110
133
  def is_number(num)
111
134
  is_num = ((num.to_f.to_s == num.to_s) || (num.to_i.to_s == num.to_s))
112
135
  return is_num
113
136
  end
114
- def print_help()
137
+ def usage()
115
138
  puts """
116
- Usage(any of the following are acceptable):
117
- \tbitwiseCalc <num> <operation>
118
- \tbitwiseCalc <num> <num> <operation>
119
- \tbitwiseCalc <num> <operation> <num>\n
139
+ Usage
140
+ bitwiseCalc [num] [operation]
141
+ bitwiseCalc [num] [num] [operation]
142
+ bitwiseCalc [num] [operation] [num]
143
+ Flags
144
+ -n
145
+ disables colorized output(enabled by default)
146
+ -h
147
+ outputs usage and exits
120
148
 
121
149
  Passing <num> in a radix other than base10:
122
- <base_2_num> = 0b[0-1]
123
- <base_8_num> = 0[0-7]
124
- <base_16_num> = 0x[0-9a-f]
150
+ <base_2_num> = 0b[0-1]
151
+ <base_8_num> = 0[0-7]
152
+ <base_16_num> = 0x[0-9a-f]
125
153
 
154
+ Operations
126
155
  NOT x
127
156
  Displays ~x
128
157
  x y RAD
@@ -231,5 +260,6 @@ def main()
231
260
  runtime.parse_args()
232
261
  runtime.check_for_errors()
233
262
  runtime.execute()
263
+ runtime.print_results()
234
264
  end
235
265
  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.3.2
4
+ version: 1.4.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-07-03 00:00:00.000000000 Z
11
+ date: 2015-09-13 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.3
61
+ rubygems_version: 2.2.5
62
62
  signing_key:
63
63
  specification_version: 4
64
64
  summary: BitWise Calculator