class BoolCalc

Public Class Methods

new() click to toggle source
# File lib/bitwiseCalc, line 11
def initialize()
    @x = nil; @y = nil; @ans = nil; @operation = nil

    # Valid Operations
    @ops = { :NOT => "Bitwise Not", :SHR => "Shift Right",
             :SHL => "Shift Left", :AND => "Logical And",
             :OR => "Logical Or", :XOR => "Exclusive Or",
             :RAD => "Display Radixes", :ROL => "Circular Rotate Left",
             :ROR => "Circular Rotate Right", :MUL => "Multiply",
             :DIV => " Division", :ADD => "Addition",
             :SUB => "Subtraction", :MOD => "Modulus",
             :POW => "Exponentiation", :NOR => "Negated OR",
             :NAND => "Negated AND", :XNOR => "Complement XOR"}

    # Option Hash
    @options = { nocolor: false, grouped: false, bit4: true,
                 bit64: true, ipnum: false, nargs: false }
end

Public Instance Methods

check_for_errors() click to toggle source

Detect any errors before operation execution

# File lib/bitwiseCalc, line 106
def check_for_errors()
    # no operation read
    abort("ERROR, Expected Operation".red) if @operation.nil?

    # make sure binary operations have binary input
    restriction = @operation != "NOT" && @operation != "RAD" && @y.nil?
    # also make sure we have read at least 1 number
    if restriction || @x.nil?
        abort("ERROR, Expected Number(s) for Operation:\t#{@operation}".red)
    end

    # div by zero
    if (@operation == "DIV" || @operation == "MOD") && @y == 0
        abort("ERROR, Divide by 0 for Operation:\t#{@operation}".red)
    end
end
execute() click to toggle source

Executes desired operation.

# File lib/bitwiseCalc, line 42
def execute()
    case @operation
    when 'RAD'
        return
    when 'AND'
        @ans = @x & @y
    when 'NAND'
        @ans = ~(@x & @y)
    when 'OR'
        @ans = @x | @y
    when 'NOR'
        @ans = ~(@x | @y)
    when 'XOR'
        @ans = @x ^ @y
    when 'XNOR'
        @ans = ~(@x ^ @y)
    when 'NOT'
        @ans = ~@x
    when 'SHR'
        @ans = @x >> @y
    when 'SHL'
        @ans = @x << @y
    when 'ROL'
        binarr = int_to_binarr(@x)
        @ans = rol(binarr).join.to_i(2)
    when 'ROR'
        binarr = int_to_binarr(@x)
        @ans = ror(binarr).join.to_i(2)
    when 'MUL'
        @ans = @x * @y
    when 'ADD'
        @ans = @x + @y
    when 'DIV'
        @ans = @x / @y
    when 'SUB'
        @ans = @x - @y
    when 'MOD'
        @ans = @x % @y
    when 'POW'
        @ans = @x ** @y
    end
end
int_to_binarr(arr) click to toggle source

Converts an integer to a binary array

# File lib/bitwiseCalc, line 237
def int_to_binarr(arr)
    tmparr = []
    255.downto(0) do |n|
        tmparr << arr[n]
    end
    tmparr
end
parse_args() click to toggle source

Parse arguments passed.

# File lib/bitwiseCalc, line 124
def parse_args()
    #exit if no args passed; parse flags only if needed
    usage if ARGV.empty?
    parse_flags if ARGV[0][0] == "-"

    ARGV.each do |arg|

        case arg
        #check if valid operation choice
        when arg[/[a-zA-Z]+/]
            @operation = arg.upcase if @ops.has_key?(arg.upcase.to_sym)
        #hex number
        when arg[/0[xX][0-9a-fA-F]+/]
            @x == nil ? @x = arg.to_i(16) : @y = arg.to_i(16)
        #binary number
        when arg[/0[bB][0-1]+/]
            @x == nil ? @x = arg.to_i(2) : @y = arg.to_i(2)
        #octal number
        when arg[/0[0-7]+/]
            @x == nil ? @x = arg.to_i(8) : @y = arg.to_i(8)
        #decimal number
    when arg[/^-?[0-9]+/]
            @x == nil ? @x = arg.to_i : @y = arg.to_i
        end
    end
end
parse_flags() click to toggle source

Parses flags, only called if flags passed are detected.

# File lib/bitwiseCalc, line 151
def parse_flags()
    flags = ARGV[0]
    flags.split("").each do |flag|
        case flag
        when "n"
            @options[:nocolor] = true
        when "h"
            usage
        when "f"
            @options[:bit4] = false
        when "s"
            @options[:bit64] = false
        when "g"
            @options[:grouped] = true
        end
    end
end
print_all_radix(num) click to toggle source

Outputs num in radixes mentioned in program description.

print_nice_binary(num) click to toggle source

Outputs number in Binary form for easier reading Will print in increments of 32,64,128, and then 256 Will print 64 bits before moving to the next line

print_results() click to toggle source

Neatly displays results

rol(tmparr) click to toggle source

Rotate left implementation

# File lib/bitwiseCalc, line 266
def rol(tmparr)
    arr_tail = tmparr.length-2
    for i in 1..@y
        #save most significant bit(beginning of array)
        msb = tmparr[0]
        for j in 0..arr_tail
            #assign next element in array to current element in array
            tmparr[j] = tmparr[j+1]
        end
        #assign most significant bit to tail
        tmparr[-1] = msb
    end
    tmparr
end
ror(tmparr) click to toggle source

Rotate right implementation

# File lib/bitwiseCalc, line 245
def ror(tmparr)
    arr_tail = tmparr.length-2
    for i in 1..@y
        #save least significant bit(end of array)
        lsb = tmparr[-1]
        for j in 0..arr_tail
            if j == 0
                tmp1 = tmparr[j+1]
                tmparr[j+1] = tmparr[j]
            end
            #save next element, make next element previously saved elem
            tmp2 = tmparr[j+1]
            tmparr[j+1] = tmp1
            tmp1 = tmp2
        end
        #assign least significant bit(end of array) to head of array
        tmparr[0] = lsb
    end
    tmparr
end
usage() click to toggle source
# File lib/bitwiseCalc, line 169
def usage()
    puts """
    Description:
        A simple bitwise calculator that executes the operations noted below.
        It can accept Octal, Hexadecimal, Decimal, and Binary inputs.
        It will output in above mentioned radixes.
        Binary output will have 32, 64, 128, and up to 256 bit format.
        The results are printed to the console.
    Usage:
        Pass arguments to the program as shown below. Note that binary output
        is spaced every 4 bits. Every 64 bits, a newline is added for readability.
        For additional help/examples:
            https://github.com/vargash1/Bitwise-Calculator

        Unary Operations:
            bitwiseCalc [flag] <number> <operation>

        Binary Operations:
            bitwiseCalc [flag] <number> <operation> <number>

        Passing a number in a radix other than base10:
            <base_2_number>  = 0b[0-1]
            <base_8_number>  = 0[0-7]
            <base_16_number> = 0x[0-9a-f]
    Flags:
        -f
            disables spacing every 4 bits in binary output(enabled by default)
        -s
            disables newline every 64 bits in binary output(enabled by default)
        -n
            disables colorized output(enabled by default)
        -h
            displays this message and exits
        -g
            outputs base 10 numbers in grouped form(with commas)
    Operations:"""
    @ops.each_pair do |key,val|
        puts "\t #{key}\n\t\t#{val}"
    end
    exit
end