- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个 Swift 框架来处理大(256 位)数字,因此我创建了一个名为 UInt256 的结构。我尝试使其符合FixedWidthInteger、UnsignedInteger 和BinaryInteger,以便它可以用作常规的Swift 整数类型。我的问题是打印它。每当我创建具有特定值的 UInt256 时,它都工作得很好。当我尝试打印它时,它出于某种原因调用 UInt256.init(integerLiteral:) ,这似乎将其放入无限循环中,最终导致 EXC_BAD_ACCESS 错误。
首先,我回到了之前,我让它符合所有整数协议(protocol)并且它工作得很好。我想知道为什么我不能在没有这个错误的情况下成功地符合它。
我分析了线程崩溃时的情况,每当它尝试打印对象时,它都会调用值为 0 的 UInt256.init(integerLiteral:) 命令。
我注意到的一件有趣的事情是该错误与数组和数组缓冲区有关。在该 init 函数中,我尝试创建一个无用的数组,但它总是在包含数组初始值设定项的第一行崩溃。这是否可能是因为在重复循环之后,数组缓冲区的空间不足?
我尝试在可疑的 init 函数中添加一条打印语句,并尝试打印传递给它的值。果然,测试打印了 100 次“0”,然后崩溃了。
以下是将其粘贴到 Playground 所需的所有代码:
import Foundation
/// Adds a character and a string
///
/// - Parameters:
/// - left: String
/// - right: Character
///
/// - Returns: The string with the appended character
public func +(left: String, right: Character) -> String {
var newString = left
newString.append(right)
return newString
}
/// Adds a character to a given string, and assigns the new string to the original
///
/// - Parameters:
/// - left: inout String
/// - right: Character
///
/// - Returns: The string with the appended character
@discardableResult public func +=(left: inout String, right: Character) -> String {
left = left + right
return left
}
extension String {
// MARK: Subscripts
/// References the ith character of a string
subscript (i: Int) -> Character {
return self[index(startIndex, offsetBy: i)]
}
}
/// The size of the big number
public let BN_SIZE = 4
/// Size of BN in Bytes
public let BN_SIZE_IN_BYTES = MemoryLayout<UInt64>.size * BN_SIZE
/// Size of a BN String
public let BN_STR_SIZE = (2 * BN_SIZE_IN_BYTES + 1)
/// An unsigned 256-bit integer, sometimes denoted as just 'BN', or a BigNumber
public struct UInt256: CustomDebugStringConvertible, FixedWidthInteger, UnsignedInteger {
// MARK: Static properties
/// It's just 0, expressed as a UInt256
public static let zero = BigNumber()
// MARK: Properties
/// The BN as a hex string
public var hexString: String {
var string = ""
let bnSizeCountdown = Array(1...BN_SIZE).reversed()
for i in bnSizeCountdown {
let uint64IndexCountdown = Array(1...16).reversed()
for j in uint64IndexCountdown {
string += toChar(self[i - 1] >> ((j - 1) * 4))
}
}
return string
}
/// The hex representation of the BN, used for debugging
public var debugDescription: String {
return hexString
}
/// The array of UInt64's representing the BN. This is a fixed size of BN_SIZE
public var array: [UInt64]
// MARK: Initializers
public init(integerLiteral value: UInt) {
self.array = [UInt64](repeating: 0, count: BN_SIZE) // THIS is the line that gives me the error: EXC_BAD_ACCESS (code=2, address=0x7ffeef3ffff8)
self[0] = UInt64(value)
}
public init(_truncatingBits bits: UInt) {
self.array = UInt256.from(int: bits).array
}
/// Creates a BN from an array of UInt64's
///
/// - Parameters:
/// - array: An array of UInt64's
///
/// - Returns: Nil if the array size is incorrect, but otherwise returns the corresponding BN
public init?(array: [UInt64]) {
if array.count != BN_SIZE { return nil }
self.array = array
}
/// Creates a BN from a hex string
///
/// - Parameters:
/// - hex: A hex string to be converted to BN
///
/// - Returns: Returns nil if hex string is invalid
public init?(hexString hex: String) {
self.array = [UInt64](repeating: 0, count: BN_SIZE)
for i in 0..<hex.count {
if toNibble(hex[i]) == 16 { return nil }
let reversedSequence = (1..<BN_SIZE).reversed()
for j in reversedSequence {
self[j] <<= 4
self[j] |= (self[j - 1] >> 60) & 0x0f
}
self[0] <<= 4
self[0] |= UInt64(toNibble(hex[i]) & 0x0f)
}
}
/// Creates a BN of value 0
public init() {
self.array = [UInt64](repeating: 0, count: BN_SIZE)
}
// MARK: Static Functions
/// Converts an Integer to a BigNumber
///
/// - Parameters:
/// - int: An UnsignedInteger to convert to a BigNumber
///
/// - Returns: A BigNumber
public static func from<T: UnsignedInteger>(int: T) -> BigNumber {
var bn = BigNumber()
bn[0] = UInt64(int)
return bn
}
// MARK: Enumerations
// MARK: Subscripts
/// Gets or sets a value for the given index in the BN array representation
public subscript(i: Int) -> UInt64 {
get {
return array[i]
} set(newValue) {
array[i] = newValue
}
}
// MARK: Conformance Requirements
public static var bitWidth: Int {
return BN_SIZE_IN_BYTES * 8
}
public typealias Words = UInt64.Words
public typealias IntegerLiteralType = UInt
public func addingReportingOverflow(_ rhs: UInt256) -> (partialValue: UInt256, overflow: Bool) {
return (self + rhs, (self > (self + rhs) ? false : true))
}
public func subtractingReportingOverflow(_ rhs: UInt256) -> (partialValue: UInt256, overflow: Bool) {
return (self + rhs, (self < (self - rhs) ? false : true))
}
public func multipliedReportingOverflow(by rhs: UInt256) -> (partialValue: UInt256, overflow: Bool) {
return (self * rhs, (self > (self * rhs) ? false : true))
}
public func dividedReportingOverflow(by rhs: UInt256) -> (partialValue: UInt256, overflow: Bool) {
// don't trust this, I don't even know what this does
return (self / rhs, (self > (self / rhs) ? false : true))
}
public func remainderReportingOverflow(dividingBy rhs: UInt256) -> (partialValue: UInt256, overflow: Bool) {
// I don't know how this would result in an overflow, but whatever
return (self % rhs, false)
}
public func multipliedFullWidth(by other: UInt256) -> (high: UInt256, low: UInt256) {
// what does this even mean? Seriously, if someone else would like to write this part, that'd be great
return (self, self)
}
public func dividingFullWidth(_ dividend: (high: UInt256, low: UInt256)) -> (quotient: UInt256, remainder: UInt256) {
// what does this even mean? Seriously, if someone else would like to write this part, that'd be great
return (self, self)
}
public var nonzeroBitCount: Int {
var nonzeros: Int = 0
for i in 0..<BN_SIZE {
nonzeros += self[i].nonzeroBitCount
}
return nonzeros
}
public var leadingZeroBitCount: Int {
var leadingZeros: Int = self[3].leadingZeroBitCount
for i in (0..<(BN_SIZE-1)).reversed() {
if self[i+1].nonzeroBitCount > 0 {
break
}
leadingZeros += self[i].leadingZeroBitCount
}
return leadingZeros
}
public var byteSwapped: UInt256 {
let reversedArray = self.array.reversed()
return UInt256(array: reversedArray.map { $0.byteSwapped })!
}
public var words: UInt64.Words {
// I have no idea what this variable is supposed to be.
// someone else please do this
return UInt64.Words(8)
}
public var trailingZeroBitCount: Int {
// I couldn't be bothered to write this right now.
// I'm in the car after a long day and I am tired
// I just want this project to build so I can say I had a good day and go to sleep
return 0
}
// MARK: Private Methods
/// Converts an ascii hex character to a nibble
func toNibble(_ char: Character) -> UInt8 {
let n = UInt64(char.asciiValue!)
return ((n >= 0x30) && (n <= 0x39)) ? UInt8(n - 0x30):
((n >= 0x41) && (n <= 0x46)) ? UInt8(n - 0x37):
((n >= 0x61) && (n <= 0x66)) ? UInt8(n - 0x57):
0x10
}
/// Converts a nibble to a hex character
func toChar(_ nibble: UInt64) -> Character {
var c: Character
let n = 0x0f&nibble
if 0 <= n && n < 10 {
c = String(n)[0]
} else {
switch n {
case 10:
c = "A"
case 11:
c = "B"
case 12:
c = "C"
case 13:
c = "D"
case 14:
c = "E"
case 15:
c = "F"
case 255:
c = "F"
case 16:
c = "0"
default:
c = "X"
}
}
return c
}
// MARK: Comparisons
/// Checks if two BNs are equal
///
/// - Parameters:
/// - a: A BN
/// - b: Another BN
///
/// - Returns: True if they are equal, false if not
public static func == (a: BN, b: BN) -> Bool {
for i in 0..<BN_SIZE {
if a[i] != b[i] {
return false
}
}
return true
}
/// Checks if two BNs are unequal
///
/// - Parameters:
/// - a: A BN
/// - b: Another BN
///
/// - Returns: True if they are unequal, false if not
public static func != (a: BN, b: BN) -> Bool {
return !(a == b)
}
/// Checks if a BN is smaller than another BN
///
/// - Parameters:
/// - a: The number that is checked to be smaller
/// - b: The number that is checked to be larger
///
/// - Returns: True if a < b, false if otherwise
public static func < (a: BN, b: BN) -> Bool {
if a == b { return false }
for i in 0..<BN_SIZE {
if a[3-i] > b[3-i] { return false }
}
return true
}
/// Checks if a BN is larger than another BN
///
/// - Parameters:
/// - a: The number that is checked to be larger
/// - b: The number that is checked to be smaller
///
/// - Returns: True if a < b, false if otherwise
public static func > (a: BN, b: BN) -> Bool {
if a == b { return false }
for i in 0..<BN_SIZE {
if a[3-i] < b[3-i] { return false }
}
return true
}
/// Greater than OR equal to
///
/// - Parameters:
/// - a: A BN
/// - b: Another BN
///
/// - Returns: TRUE if a >= b, false if otherwise
public static func >= (a: BN, b: BN) -> Bool {
return a == b || a > b
}
/// Less than OR equal to
///
/// - Parameters:
/// - a: A BN
/// - b: Another BN
///
/// - Returns: TRUE if a <= b, false if otherwise
public static func <= (a: BN, b: BN) -> Bool {
return a == b || a < b
}
// MARK: Arithemetic
/// Adds two BigNumbers
///
/// - Parameters:
/// - a: Left BN
/// - b: Right BN
///
/// - Returns: Sum of left BN and right BN
public static func + (a: BigNumber, b: BigNumber) -> BigNumber {
var r = BigNumber()
var carry_out: UInt64 = 0
var carry_in: UInt64 = 0
for i in 0..<BN_SIZE {
r[i] = a[i] &+ b[i]
if (r[i] < a[i]) {
carry_out = 1
}
if (carry_in != 0) {
r[i] &+= 1
if ( 0 == r[i]) {
carry_out = 1
}
}
carry_in = carry_out
carry_out = 0
}
return r
}
/// Subtracts BigNumber b from BigNumber a
///
/// If the subtraction will result in a negative number, there will be an overflow error
///
/// This uses Two's Complement subtraction
///
/// - Parameters:
/// - a: Left BigNumber
/// - b: Right BigNumber
///
/// - Returns: Difference of parameters
public static func - (a: BigNumber, b: BigNumber) -> BigNumber {
// First, calculate two's complement of B
var b_t = b
for i in 0..<b_t.array.count {
b_t.array[i] = ~b_t.array[i]
}
b_t = b_t + UInt256.from(int: UInt(1))
return a + b_t
}
/// Multiplies two BNs
///
/// - Parameters:
/// - a: A BN Factor
/// - b: Another BN Factor
///
/// - Returns: The product
public static func * (a: BigNumber, b: BigNumber) -> BigNumber {
var p = BN()
let multiplier = a < b ? a : b
let adder = a >= b ? a : b
var index = multiplier
let zero = BN()
while index != zero {
p = p + adder
index = index - UInt256.from(int: UInt(1))
}
return p
}
/// Divides BN a by BN b, and returns the quotient
///
/// - Parameters:
/// - a: Dividend
/// - b: Divisor
///
/// - Returns: The quotient
public static func / (a: BN, b: BN) -> BN {
if b == BN.zero { fatalError("Dude you can't divide by zero in OUR universe") }
var q = BN.zero
var dividend = a
while (dividend >= b) {
q = q + UInt256.from(int: UInt(1))
dividend = dividend - b
}
return q
}
/// Modulo operator
///
/// - Parameters:
/// - a: Dividend
/// - b: Divisor
///
/// - Returns: The remainder as a BN after division
public static func % (a: BN, b: BN) -> BN {
if b == BN.zero { fatalError("Dividing by zero is not possible, sorry!") }
var dividend = a
while (dividend >= b) {
dividend = dividend - b
}
return dividend
}
// MARK: Bitwise operators
/// Bitwise AND operator
///
/// - Parameters:
/// - a: A UInt256
/// - b: Another UInt256
///
/// - Returns: The bitwise AND of the two values
public static func & (a: UInt256, b: UInt256) -> UInt256 {
var ANDarray: [UInt64] = a.array
for i in 0..<ANDarray.count {
ANDarray[i] &= b[i]
}
return UInt256(array: ANDarray)!
}
/// Bitwise OR operator
///
/// - Parameters:
/// - a: A UInt256
/// - b: Another UInt256
///
/// - Returns: The bitwise OR of the two values
public static func | (a: UInt256, b: UInt256) -> UInt256 {
var ANDarray: [UInt64] = a.array
for i in 0..<ANDarray.count {
ANDarray[i] |= b[i]
}
return UInt256(array: ANDarray)!
}
/// Bitwise XOR operator
///
/// - Parameters:
/// - a: A UInt256
/// - b: Another UInt256
///
/// - Returns: The bitwise XOR of the two values
public static func ^ (a: UInt256, b: UInt256) -> UInt256 {
var ANDarray: [UInt64] = a.array
for i in 0..<ANDarray.count {
ANDarray[i] ^= b[i]
}
return UInt256(array: ANDarray)!
}
// MARK: Assignment operators
public static func *= (lhs: inout UInt256, rhs: UInt256) {
lhs = lhs * rhs
}
public static func += (lhs: inout UInt256, rhs: UInt256) {
lhs = lhs + rhs
}
public static func -= (lhs: inout UInt256, rhs: UInt256) {
lhs = lhs - rhs
}
public static func /= (lhs: inout UInt256, rhs: UInt256) {
lhs = lhs / rhs
}
public static func %= (lhs: inout UInt256, rhs: UInt256) {
lhs = lhs % rhs
}
public static func &= (lhs: inout UInt256, rhs: UInt256) {
lhs = lhs & rhs
}
public static func |= (lhs: inout UInt256, rhs: UInt256) {
lhs = lhs | rhs
}
public static func ^= (lhs: inout UInt256, rhs: UInt256) {
lhs = lhs ^ rhs
}
public func hash(into hasher: inout Hasher) {
for uint64 in array {
hasher.combine(uint64)
}
}
}
/// For convenience, a UInt256 can be known as a BN
public typealias BN = UInt256
/// For convenience, a UInt256 can be known as a BigNumber
public typealias BigNumber = UInt256
这是测试代码:
let a: UInt256 = 42
print(a)
您想查看的任何其他代码都可以在 the GitHub page 找到
测试的预期结果应该是“2A”(十六进制的 42),后面跟着一堆 0。相反,它只是崩溃了。
这个错误一直让我头疼,因为我完全不知道从哪里开始。任何帮助将不胜感激。
最佳答案
将print(a)
更改为debugPrint(a)
。您有一个连贯的 debugDescription
实现,因此调用 debugPrint
将会起作用。
关于swift - 为什么打印对象的实例会使其陷入无限循环,并因错误 EXC_BAD_ACCESS 而崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56640046/
我是 JavaFx 新手,正在尝试编写我的刽子手游戏。我现在必须检查保存我的词典的文件是否在那里。如果没有,请用户输入其位置。我试图在弹出窗口中执行此操作,要求用户输入位置并点击按钮,该按钮反过来将导
我卡住了。我试图用 SQLite 表的字段填充 ListView 。我对 Android 编程很陌生。关于我为什么会收到此错误的任何建议: 10-24 10:24:31.154: ERROR/Andr
我正在编写一个 java 程序,它接受用户输入的数字 x,对从 1 到 x(包括 x)的所有可被 3 整除的数字求和,并显示总和。它编译没有错误但是当我执行程序时,它陷入循环并继续执行直到我关闭命令提
JS 和 Web 编程新手。 我有一个图像在 Canvas 上沿水平轴移动。当图像到达某个点时,我希望它停止、缩放然后旋转。我使用的方法会导致无限循环,我不明白为什么。 这是被调用的方法 while
我制作了一个使用表单、php 和 javascript 进行登录和注销的页面。 但唯一的问题是,当您提交表单并且数据发生更改时,直到页面重新加载后才会更新。 注意lo
我在查询时陷入困境。我想根据“时间和点赞数”查询帖子,但不知道如何在 firebase 上查询。示例: Query mquery =FireBaseDatabase() getReference().
这个问题已经有答案了: Why does my ArrayList contain N copies of the last item added to the list? (5 个回答) 已关闭 6
所以,我有一个项目,任务是这样表述的: 您需要创建一个名为 Candidate 的类,它存储参加选举的人的姓氏以及他们获得的票数。您还应该有访问每个字段的方法。此时,这些字段不需要修改器,因为它们不应
我正在解决 Java 编程类(class)中的作业问题,但我陷入了困境。我会预先告诉你,我绝不是专家,并且对 Java 或一般编程了解不多。 我遇到的问题实际上不是我的作业问题,而是书中的一个例子,我
我正在用java编写一个客户端程序。服务器是用C++编写的。当我从以下位置发送消息时客户端到服务器就可以了。但是在接收过程中我的客户端线程陷入了这个调用 in = new BufferedReader
我正在参加 Edx 上的在线类(class) cs50,我有一个作业,其中我必须创建一个程序,用户在其中输入关键字(然后用于加密)和需要在 Vigenere 中加密的字符串。密码。 Vigenere
我遇到了陷入 getIntLimited 函数的问题。在调整数量中,我需要它检查用户是否输入了正确的数字,而不是多于或少于所需的数字,而不是字母。我没有对“库存”选项这样做,只在“检查”选项中这样做。
我更喜欢创建一个 Dictionary 对象并向其中添加 3 个单词。我的程序没有编译错误,但在第二个 for 循环中出现运行时错误,问题出在 addNewWord 函数中吗?我需要传递一个指向 Di
该程序的要点是获取 2 个文件,一个是字典,另一个是文本,创建一个输出文件,并将文本文件中的所有单词放入其中,但将文本文件中不存在的单词大写字典。 当我运行程序时,它不断要求输入,所以看起来我陷入了
我正在为我的 C++ 类(class)做作业。在这个作业中,我需要编写一个程序,从 cin 中读取数字,然后对它们求和,当使用 while 循环输入 0 时停止。 我已经编写了代码并获得了我需要的结果
这是我到目前为止所得到的- $awards_sql_1 = mysql_query('SELECT * FROM categories WHERE section_id = 1') or die(my
我想弄清楚某个步骤何时发生。所以我编写了一个名为 countSteps 的方法来执行此操作。它的问题是我陷入其中,因为在 while 循环中我不断获取新数据,而且我认为它永远不会返回到 onSenso
我有点卡在一个问题上了。我有一个 Spring + Hibernate 应用程序,最近几天表现得很奇怪。 通常,即使在 Debug模式下,它也会引导大约 15 秒。 几天后,在没有显示任何重大错误或问
我是编程新手,我想知道是否有人可以帮助我解决这个问题?它似乎处于连续循环中,我一直在更改它并尝试不同的方法近一个小时,将不胜感激。 #include "stdafx.h" #include
我正在用 Python 创建一个聊天服务器,并且对这门语言的了解还很浅。我现在有 1 个问题,我想在进一步解决之前解决,但我似乎找不到解决问题的方法。 这是一个持续的 while 循环.. 下面的代码
我是一名优秀的程序员,十分优秀!