help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
swift-ios-calculator
RSK World
swift-ios-calculator
Swift iOS Calculator v1.0 - AI Math Solver + 3D Graphing + Apple Watch Integration + iOS Widgets + Siri Shortcuts + Currency Converter + Scientific Calculator + Matrix Operations + Platform Integration + Modern iOS Development
swift-ios-calculator
  • Assets.xcassets
  • Base.lproj
  • swift-ios-calculator.xcodeproj
  • AIMathSolverViewController.swift26.6 KB
  • AppDelegate.swift1.7 KB
  • CalculatorHistoryViewController.swift6.5 KB
  • CalculatorLogic.swift4.4 KB
  • CalculatorSettingsViewController.swift5.6 KB
  • CalculatorTheme.swift8.3 KB
  • CalculatorUtils.swift6.9 KB
  • CalculatorViewController.swift3.4 KB
  • CalculatorWidget.swift14.3 KB
  • CalculatorWidgetInfo.plist846 B
  • ChemistryCalculatorViewController.swift26.9 KB
  • CurrencyConverterViewController.swift13.3 KB
  • CustomFormulaBuilderViewController.swift22.1 KB
  • EngineeringCalculatorViewController.swift25.7 KB
  • EquationSolverViewController.swift22.8 KB
  • FinancialCalculatorViewController.swift27.5 KB
  • GeometryCalculatorViewController.swift29.7 KB
  • Graphing3DViewController.swift20.8 KB
  • GraphingCalculatorViewController.swift14.7 KB
  • Info.plist3.2 KB
  • LICENSE1.1 KB
  • MainTabBarController.swift22.3 KB
  • MatrixCalculatorViewController.swift26.6 KB
  • PRIVACY_POLICY.md1.6 KB
  • PhysicsCalculatorService.swift8.2 KB
  • ProgrammerCalculatorViewController.swift11 KB
  • README.md8.7 KB
  • RELEASE_NOTES.md6.5 KB
  • SceneDelegate.swift2.8 KB
  • ScientificCalculatorViewController.swift6.2 KB
  • SiriShortcutsManager.swift23.5 KB
  • Swift iOS Calculator.entitlements1.1 KB
  • UnitConverterViewController.swift14 KB
  • WatchCalculatorViewController.swift15.3 KB
  • index.html47.5 KB
ProgrammerCalculatorViewController.swift
ProgrammerCalculatorViewController.swift
Raw Download
Find: Go to:
//
//  ProgrammerCalculatorViewController.swift
//  Swift iOS Calculator
//
//  Created by RSK World on 23/01/2026.
//  Copyright © 2026 RSK World. All rights reserved.
//
//  Developer: Molla Samser (Founder, RSK World)
//  Designer & Tester: Rima Khatun
//  Contact: info@rskworld.com, +91 93305 39277
//  Website: https://rskworld.in
//  Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India - 713147
//

import UIKit

class ProgrammerCalculatorViewController: UIViewController {
    
    // MARK: - Outlets
    @IBOutlet weak var decimalDisplay: UITextField!
    @IBOutlet weak var binaryDisplay: UITextField!
    @IBOutlet weak var hexadecimalDisplay: UITextField!
    @IBOutlet weak var octalDisplay: UITextField!
    @IBOutlet weak var baseSegmentedControl: UISegmentedControl!
    @IBOutlet weak var bitLengthSegmentedControl: UISegmentedControl!
    @IBOutlet weak var signedSwitch: UISwitch!
    
    // MARK: - Properties
    private var currentValue: Int64 = 0
    private var currentBase: Base = .decimal
    private var bitLength: BitLength = .bits64
    private var isSigned: Bool = false
    
    enum Base: Int, CaseIterable {
        case binary = 2
        case octal = 8
        case decimal = 10
        case hexadecimal = 16
        
        var name: String {
            switch self {
            case .binary: return "BIN"
            case .octal: return "OCT"
            case .decimal: return "DEC"
            case .hexadecimal: return "HEX"
            }
        }
    }
    
    enum BitLength: Int, CaseIterable {
        case bits8 = 8
        case bits16 = 16
        case bits32 = 32
        case bits64 = 64
        
        var name: String {
            return "\(self.rawValue) bit"
        }
    }
    
    // MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        updateAllDisplays()
    }
    
    // MARK: - Setup
    private func setupUI() {
        title = "Programmer Calculator"
        view.backgroundColor = CalculatorTheme.shared.backgroundColor
        
        // Setup displays
        setupDisplay(decimalDisplay, placeholder: "Decimal")
        setupDisplay(binaryDisplay, placeholder: "Binary")
        setupDisplay(hexadecimalDisplay, placeholder: "Hexadecimal")
        setupDisplay(octalDisplay, placeholder: "Octal")
        
        // Setup segmented controls
        setupSegmentedControl(baseSegmentedControl, titles: Base.allCases.map { $0.name })
        setupSegmentedControl(bitLengthSegmentedControl, titles: BitLength.allCases.map { $0.name })
        
        // Setup signed switch
        signedSwitch.backgroundColor = CalculatorTheme.shared.buttonBackgroundColor
        signedSwitch.onTintColor = CalculatorTheme.shared.accentColor
        signedSwitch.layer.cornerRadius = 16
    }
    
    private func setupDisplay(_ textField: UITextField, placeholder: String) {
        textField.backgroundColor = CalculatorTheme.shared.displayBackgroundColor
        textField.textColor = CalculatorTheme.shared.textColor
        textField.placeholder = placeholder
        textField.textAlignment = .right
        textField.font = UIFont.monospacedSystemFont(ofSize: 18, weight: .medium)
        textField.layer.cornerRadius = 8
        textField.layer.borderWidth = 1
        textField.layer.borderColor = CalculatorTheme.shared.buttonBackgroundColor.cgColor
    }
    
    private func setupSegmentedControl(_ segmentedControl: UISegmentedControl, titles: [String]) {
        segmentedControl.removeAllSegments()
        for (index, title) in titles.enumerated() {
            segmentedControl.insertSegment(withTitle: title, at: index, animated: false)
        }
        segmentedControl.selectedSegmentIndex = 0
        segmentedControl.backgroundColor = CalculatorTheme.shared.buttonBackgroundColor
        segmentedControl.selectedSegmentTintColor = CalculatorTheme.shared.accentColor
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: CalculatorTheme.shared.textColor], for: .normal)
        segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
    }
    
    // MARK: - Actions
    @IBAction func baseChanged(_ sender: UISegmentedControl) {
        currentBase = Base(rawValue: sender.selectedSegmentIndex) ?? .decimal
        updateAllDisplays()
    }
    
    @IBAction func bitLengthChanged(_ sender: UISegmentedControl) {
        bitLength = BitLength(rawValue: sender.selectedSegmentIndex) ?? .bits64
        applyBitLength()
        updateAllDisplays()
    }
    
    @IBAction func signedChanged(_ sender: UISwitch) {
        isSigned = sender.isOn
        updateAllDisplays()
    }
    
    @IBAction func digitButtonPressed(_ sender: UIButton) {
        guard let digit = sender.titleLabel?.text else { return }
        
        let baseValue = currentBase.rawValue
        if let digitValue = Int(digit, radix: baseValue), digitValue < baseValue {
            let newValue = currentValue * Int64(baseValue) + Int64(digitValue)
            if isValidValue(newValue) {
                currentValue = newValue
                updateAllDisplays()
            }
        }
    }
    
    @IBAction func operationButtonPressed(_ sender: UIButton) {
        guard let operation = sender.titleLabel?.text else { return }
        
        switch operation {
        case "AND":
            performBitwiseOperation { $0 & $1 }
        case "OR":
            performBitwiseOperation { $0 | $1 }
        case "XOR":
            performBitwiseOperation { $0 ^ $1 }
        case "NOT":
            currentValue = ~currentValue
            updateAllDisplays()
        case "<<":
            currentValue <<= 1
            updateAllDisplays()
        case ">>":
            currentValue >>= 1
            updateAllDisplays()
        case "+":
        case "-":
        case "*":
        case "/":
            performArithmeticOperation(operation)
        default:
            break
        }
    }
    
    @IBAction func clearButtonPressed(_ sender: UIButton) {
        currentValue = 0
        updateAllDisplays()
    }
    
    @IBAction func clearAllButtonPressed(_ sender: UIButton) {
        currentValue = 0
        currentBase = .decimal
        bitLength = .bits64
        isSigned = false
        
        baseSegmentedControl.selectedSegmentIndex = Base.decimal.rawValue
        bitLengthSegmentedControl.selectedSegmentIndex = BitLength.bits64.rawValue
        signedSwitch.isOn = false
        
        updateAllDisplays()
    }
    
    // MARK: - Methods
    private func updateAllDisplays() {
        decimalDisplay.text = formatValue(currentValue, base: .decimal)
        binaryDisplay.text = formatValue(currentValue, base: .binary)
        hexadecimalDisplay.text = formatValue(currentValue, base: .hexadecimal)
        octalDisplay.text = formatValue(currentValue, base: .octal)
    }
    
    private func formatValue(_ value: Int64, base: Base) -> String {
        var displayValue = value
        
        if isSigned && base == .decimal {
            // Handle signed decimal display
            let maxValue = Int64(1) << (bitLength.rawValue - 1)
            if value >= maxValue {
                displayValue = value - (Int64(1) << bitLength.rawValue)
            }
        } else {
            // Handle unsigned display
            displayValue = value & ((Int64(1) << bitLength.rawValue) - 1)
        }
        
        switch base {
        case .binary:
            return String(displayValue, radix: 2).uppercased()
        case .octal:
            return String(displayValue, radix: 8).uppercased()
        case .decimal:
            return String(displayValue)
        case .hexadecimal:
            return String(displayValue, radix: 16).uppercased()
        }
    }
    
    private func isValidValue(_ value: Int64) -> Bool {
        let maxValue = (Int64(1) << bitLength.rawValue) - 1
        return value >= 0 && value <= maxValue
    }
    
    private func applyBitLength() {
        let mask = (Int64(1) << bitLength.rawValue) - 1
        currentValue = currentValue & mask
    }
    
    private func performBitwiseOperation(_ operation: (Int64, Int64) -> Int64) {
        // Store current value as operand
        let operand = currentValue
        currentValue = 0
        updateAllDisplays()
        
        // Wait for second operand (simplified for demo)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.currentValue = operation(operand, self.currentValue)
            self.updateAllDisplays()
        }
    }
    
    private func performArithmeticOperation(_ operation: String) {
        // Store current value as operand
        let operand = currentValue
        currentValue = 0
        updateAllDisplays()
        
        // Wait for second operand (simplified for demo)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            switch operation {
            case "+":
                self.currentValue = operand + self.currentValue
            case "-":
                self.currentValue = operand - self.currentValue
            case "*":
                self.currentValue = operand * self.currentValue
            case "/":
                if self.currentValue != 0 {
                    self.currentValue = operand / self.currentValue
                }
            default:
                break
            }
            self.updateAllDisplays()
        }
    }
}

// MARK: - Bitwise Operations Extension
extension ProgrammerCalculatorViewController {
    
    func getBitRepresentation() -> [Bool] {
        var bits = [Bool]()
        for i in 0..<bitLength.rawValue {
            let mask = Int64(1) << i
            bits.append((currentValue & mask) != 0)
        }
        return bits.reversed()
    }
    
    func setBit(at index: Int, value: Bool) {
        guard index >= 0 && index < bitLength.rawValue else { return }
        
        let mask: Int64 = value ? (1 << index) : ~(1 << index)
        if value {
            currentValue |= mask
        } else {
            currentValue &= mask
        }
        updateAllDisplays()
    }
    
    func toggleBit(at index: Int) {
        guard index >= 0 && index < bitLength.rawValue else { return }
        
        currentValue ^= (1 << index)
        updateAllDisplays()
    }
    
    func countOnes() -> Int {
        var count = 0
        var value = currentValue
        while value != 0 {
            count += Int(value & 1)
            value >>= 1
        }
        return count
    }
    
    func countZeros() -> Int {
        return bitLength.rawValue - countOnes()
    }
    
    func getTwoComplement() -> Int64 {
        return (~currentValue + 1) & ((Int64(1) << bitLength.rawValue) - 1)
    }
    
    func getOnesComplement() -> Int64 {
        return ~currentValue & ((Int64(1) << bitLength.rawValue) - 1)
    }
}
326 lines•11 KB
swift

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer