Skip to content

Commit

Permalink
Fix AND, OR, NOT & XOR operators when base is not 10
Browse files Browse the repository at this point in the history
  • Loading branch information
JALVITO committed May 26, 2020
1 parent 0a1d3aa commit 007f64d
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions BaseCalc/Number.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,25 +228,31 @@ class Number: NSObject {
static func & (leftNum: Number, rightNum: Number) throws -> Number {
let leftVal = Int(leftNum.value)
let rightVal = Int(rightNum.value)
let result = String(leftVal & rightVal)
let strResult = String(leftVal & rightVal)
let result = try Number(number: strResult, base: .Base10)

return try Number(number: result, base: rightNum.base)
result.setBase(base: rightNum.base)
return result
}

static func | (leftNum: Number, rightNum: Number) throws -> Number {
let leftVal = Int(leftNum.value)
let rightVal = Int(rightNum.value)
let result = String(leftVal | rightVal)
let strResult = String(leftVal | rightVal)
let result = try Number(number: strResult, base: .Base10)

return try Number(number: result, base: rightNum.base)
result.setBase(base: rightNum.base)
return result
}

static func ^ (leftNum: Number, rightNum: Number) throws -> Number {
let leftVal = Int(leftNum.value)
let rightVal = Int(rightNum.value)
let result = String(leftVal ^ rightVal)
let strResult = String(leftVal ^ rightVal)
let result = try Number(number: strResult, base: .Base10)

return try Number(number: result, base: rightNum.base)
result.setBase(base: rightNum.base)
return result
}

static func ~| (leftNum: Number, rightNum: Number) throws -> Number {
Expand Down

0 comments on commit 007f64d

Please sign in to comment.