两个数值异或脚本
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def convert_to_decimal(number_str):
if number_str.startswith("0b"):
return int(number_str, 2)
elif number_str.startswith("0o"):
return int(number_str, 8)
elif number_str.startswith("0x"):
return int(number_str, 16)
else:
return int(number_str)
def format_output(decimal_value):
binary = bin(decimal_value)[2:]
octal = oct(decimal_value)[2:]
hexadecimal = hex(decimal_value)[2:]
return {
"十进制 Decimal": decimal_value,
"二进制 Binary": binary,
"八进制 Octal": octal,
"十六进制 Hexadecimal": hexadecimal
}
def main():
input_1 = input("请输入第一个数值:")
input_2 = input("请输入第二个数值:")
num_1 = convert_to_decimal(input_1)
num_2 = convert_to_decimal(input_2)
result_decimal = num_1 ^ num_2
result_formatted = format_output(result_decimal)
print("\n异或结果:")
for notation, value in result_formatted.items():
print(f"{notation}: {value}")
if __name__ == "__main__":
main()
package main
import (
"fmt"
"math/big"
"strings"
)
func convertToDecimal(numberStr string) *big.Int {
numberStr = strings.ToLower(numberStr)
base := 10
if strings.HasPrefix(numberStr, "0b") {
base = 2
numberStr = numberStr[2:]
} else if strings.HasPrefix(numberStr, "0o") {
base = 8
numberStr = numberStr[2:]
} else if strings.HasPrefix(numberStr, "0x") {
base = 16
numberStr = numberStr[2:]
}
value := new(big.Int)
value, _ = value.SetString(numberStr, base)
return value
}
func main() {
var input1, input2 string
fmt.Print("请输入第一个数值:")
_, _ = fmt.Scan(&input1)
fmt.Print("请输入第二个数值:")
_, _ = fmt.Scan(&input2)
num1 := convertToDecimal(input1)
num2 := convertToDecimal(input2)
result := new(big.Int).Xor(num1, num2)
fmt.Println("\n异或结果:")
fmt.Printf("十进制 Decimal: %s\n", result.String())
fmt.Printf("二进制 Binary: %s\n", result.Text(2))
fmt.Printf("八进制 Octal: %s\n", result.Text(8))
fmt.Printf("十六进制 Hexadecimal: %s\n", result.Text(16))
}
阅读 10万+