bitwiseCalc 0.0.5
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 +7 -0
- data/lib/bitwiseCalc +186 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3d3103a5fd687a81612d73de708b9c568d3ebdf8
|
4
|
+
data.tar.gz: db05fbce25c5a9505ae41c59aabd1239b963dc2c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 364294f69ce190b3f6961e19a9dbd3b7d738eceea8d3e0121f82a8c06d626641aac45cc583ad83a1e39a584d8dea5d8faf865791f435f42c613936f1074d5bbd
|
7
|
+
data.tar.gz: a3e586b78e01d2038ce92df9921669dfd24fd03c9550fef54c94f0cbe40491024291f149efc8f138b20a18449b421f93e292b3257f17795a2642b5125518e903
|
data/lib/bitwiseCalc
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# @Author: vargash1
|
3
|
+
# @Name: Vargas, Hector
|
4
|
+
# @Email: vargash1@wit.edu
|
5
|
+
# @Date: 2015-02-23 10:23:20
|
6
|
+
# @Last Modified by: vargash1
|
7
|
+
# @Last Modified time: 2015-05-08 20:39:01
|
8
|
+
require 'colorize'
|
9
|
+
class BoolCalc
|
10
|
+
def print_all_radix(num)
|
11
|
+
puts "Decimal:\t\tHex:".yellow
|
12
|
+
puts "\t#{num.to_s(10)}\t\t#{num.to_s(16)}"
|
13
|
+
puts "Binary: ".yellow
|
14
|
+
print_nice_binary(num)
|
15
|
+
end
|
16
|
+
def execute()
|
17
|
+
puts "\t-----------------X---------------------".yellow
|
18
|
+
print_all_radix(@x)
|
19
|
+
if (@y != nil)
|
20
|
+
puts "\t-----------------Y---------------------".yellow
|
21
|
+
print_all_radix(@y)
|
22
|
+
end
|
23
|
+
puts "\t-----------------#{@operation}-------------------".yellow
|
24
|
+
case
|
25
|
+
when (@operation == 'AND')
|
26
|
+
final_result = @x & @y
|
27
|
+
when (@operation == 'OR')
|
28
|
+
final_result = @x | @y
|
29
|
+
when (@operation == 'XOR')
|
30
|
+
final_result = @x ^ @y
|
31
|
+
when (@operation == 'NOT')
|
32
|
+
final_result = ~@x
|
33
|
+
when (@operation == 'SHR')
|
34
|
+
final_result = @x >> @y
|
35
|
+
when (@operation == 'SHL')
|
36
|
+
final_result = @x << @y
|
37
|
+
when (@operation == 'ROL')
|
38
|
+
int_to_binary_arr()
|
39
|
+
rol()
|
40
|
+
final_result = (@bit64_array_x.join).to_i(2)
|
41
|
+
when(@operation == 'ROR')
|
42
|
+
int_to_binary_arr()
|
43
|
+
ror()
|
44
|
+
final_result = (@bit64_array_x.join).to_i(2)
|
45
|
+
when(@operation == 'RAD')
|
46
|
+
return
|
47
|
+
end
|
48
|
+
puts "\n\t----------------Result-----------------".yellow
|
49
|
+
print_all_radix(final_result)
|
50
|
+
end
|
51
|
+
def check_for_errors()
|
52
|
+
if (@operation == nil)
|
53
|
+
abort ("ERROR, EXPECTED AN OPERATION".red)
|
54
|
+
end
|
55
|
+
#not and rad require just one number argument
|
56
|
+
#if operation is not either and y is nil then we have a problem...
|
57
|
+
#every operation also requires at least 1 number
|
58
|
+
operation_restriction = (@operation != "NOT" && @operation != "RAD")
|
59
|
+
if ((operation_restriction && @y == nil) || (@x == nil))
|
60
|
+
abort("ERROR, MISSING NUMBER(s)".red)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
def parse_args()
|
64
|
+
ops = ['NOT','SHR','SHL','AND','OR','XOR','RAD','ROL','ROR']
|
65
|
+
i = 0
|
66
|
+
if (ARGV.length == 0 || (ARGV[0].upcase) == "-H")
|
67
|
+
print_help()
|
68
|
+
exit
|
69
|
+
end
|
70
|
+
ARGV.each do |a|
|
71
|
+
case
|
72
|
+
#if ARGV[a] is letters only,check if its a valid operation
|
73
|
+
when (a == a[/[a-zA-Z]*/])
|
74
|
+
ops.each do |ops_elem|
|
75
|
+
if (a.upcase == ops_elem)
|
76
|
+
@operation = ops_elem
|
77
|
+
end
|
78
|
+
end
|
79
|
+
#if ARGV[a] is a number, save it.
|
80
|
+
#also keep track of how many numbers we have read
|
81
|
+
when (is_number(a.to_i))
|
82
|
+
if (i == 0) ? @x = a.to_i: @y = a.to_i
|
83
|
+
i += 1
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
def is_number(num)
|
89
|
+
is_num = ((num.to_f.to_s == num.to_s) || (num.to_i.to_s == num.to_s))
|
90
|
+
return is_num
|
91
|
+
end
|
92
|
+
def print_help()
|
93
|
+
puts """\tSimply run the Ruby Script with the any of the following as arguements.\n
|
94
|
+
\tbitwiseCalc <num> <operation> <num>\n
|
95
|
+
Results will be output in Hex, Binary, and Decimal
|
96
|
+
\tHere are some examples
|
97
|
+
1 AND 2
|
98
|
+
Displays 1 & 2
|
99
|
+
1 OR 2
|
100
|
+
Displays 1 | 2
|
101
|
+
NOT 1
|
102
|
+
Displays ~1
|
103
|
+
1 XOR 2
|
104
|
+
Displays 1 XOR 2
|
105
|
+
1 SHR 2
|
106
|
+
Displays 1 Shifted Right by 2
|
107
|
+
1 SHL 2
|
108
|
+
Displays 1 Shifted Left by 2
|
109
|
+
1 ROR 2
|
110
|
+
Displays 1 Rotated Right 2 Times
|
111
|
+
1 ROL 2
|
112
|
+
Displays 1 Rotated Right 2 Times
|
113
|
+
1 RAD
|
114
|
+
Displays 1 in Hex, Binary(32 & 64 Bit Format), and as well as Decimal
|
115
|
+
Please Note that the order in which you enter the arguments
|
116
|
+
as well as case sensitivity DO NOT matter!
|
117
|
+
"""
|
118
|
+
exit
|
119
|
+
end
|
120
|
+
#outputs num in binary form
|
121
|
+
def print_nice_binary(num)
|
122
|
+
puts "\t32 BIT FORMAT".yellow
|
123
|
+
print "\t"
|
124
|
+
31.downto(0) do |n|
|
125
|
+
print num[n]
|
126
|
+
if (n%4 == 0); print " " end
|
127
|
+
end
|
128
|
+
puts "\n\t64 BIT FORMAT".yellow
|
129
|
+
print "\t"
|
130
|
+
63.downto(0) do |n|
|
131
|
+
print num[n]
|
132
|
+
if (n%4 == 0); print " " end
|
133
|
+
end
|
134
|
+
puts
|
135
|
+
end
|
136
|
+
#converts an integer to a binary array
|
137
|
+
#used for rotating
|
138
|
+
def int_to_binary_arr()
|
139
|
+
@bit64_array_x = []
|
140
|
+
63.downto(0) do |n|
|
141
|
+
@bit64_array_x << @x[n]
|
142
|
+
end
|
143
|
+
end
|
144
|
+
#for rotating we only need 64 bit verisions
|
145
|
+
def ror()
|
146
|
+
for i in 1..@y
|
147
|
+
#save least significant bit(end of array)
|
148
|
+
lsb = @bit64_array_x[-1]
|
149
|
+
#since we only want to change all elems besides the last
|
150
|
+
for j in 0..(@bit64_array_x.length-2)
|
151
|
+
if j == 0
|
152
|
+
#save next elem(right shift)
|
153
|
+
#assign current elem to next elem
|
154
|
+
tmp1 = @bit64_array_x[j+1]
|
155
|
+
@bit64_array_x[j+1] = @bit64_array_x[j]
|
156
|
+
end
|
157
|
+
#save next elem, make next elem previously saved elem
|
158
|
+
tmp2 = @bit64_array_x[j+1]
|
159
|
+
@bit64_array_x[j+1] = tmp1
|
160
|
+
tmp1 = tmp2 #refresh var
|
161
|
+
end
|
162
|
+
#assign least significant bit(end of array) to head of array
|
163
|
+
@bit64_array_x[0] = lsb
|
164
|
+
end
|
165
|
+
end
|
166
|
+
def rol()
|
167
|
+
for i in 1..@y
|
168
|
+
#save most significant bit(beginning of array)
|
169
|
+
msb = @bit64_array_x[0]
|
170
|
+
for j in 0..(@bit64_array_x.length-2)
|
171
|
+
#assign next element in array to current element in array
|
172
|
+
#no need to save as no data is lost(msb will save first array elem)
|
173
|
+
@bit64_array_x[j] = @bit64_array_x[j+1]
|
174
|
+
end
|
175
|
+
#assign most significant bit to tail
|
176
|
+
@bit64_array_x[-1] = msb
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
def main()
|
181
|
+
runtime = BoolCalc.new()
|
182
|
+
runtime.parse_args()
|
183
|
+
runtime.check_for_errors()
|
184
|
+
runtime.execute()
|
185
|
+
end
|
186
|
+
main()
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bitwiseCalc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hector Vargas
|
8
|
+
autorequire:
|
9
|
+
bindir: lib
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: colorize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.7.7
|
20
|
+
- - '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.7.7
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.7.7
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.7.7
|
33
|
+
description: A simple BitWise Calculator
|
34
|
+
email: hjvargas1213@gmail.com
|
35
|
+
executables:
|
36
|
+
- bitwiseCalc
|
37
|
+
extensions: []
|
38
|
+
extra_rdoc_files: []
|
39
|
+
files:
|
40
|
+
- lib/bitwiseCalc
|
41
|
+
homepage: https://github.com/vargash1/Bitwise-Calculator
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata: {}
|
45
|
+
post_install_message: Thanks for installing!
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.1.11
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: BitWise Calculator
|
65
|
+
test_files: []
|