快速通用功能中的位移位

时间:2022-11-25 16:53:08

I am trying to write a generic function that requires bit shifting operations. I am getting behavior that I don't understand. Here is a simple function that demonstrates the problem.

我正在尝试编写一个需要位移操作的通用函数。我得到了我不理解的行为。这是一个演示问题的简单函数。

func testBytes<T: IntegerType>(bytesIn: [UInt8], inout dataOut: T){

let outputSize = sizeof(T)
var temp: T = 0
dataOut = 0
temp = bytesIn[0] as T
temp = temp << 1

}

If I do this, then the last line gives me an error in xcode "T is not convertible to Int".

如果我这样做,那么最后一行在xcode中给出了一个错误“T不能转换为Int”。

I can change the last line to temp = temp << (1 as T)

我可以将最后一行更改为temp = temp <<(1作为T)

And then the error for this line changes to "T is not convertible to UInt8"

然后此行的错误更改为“T不可转换为UInt8”

Neither one of these error messages make sense to me in this context. Is there something I can do to enable bit shifting on a generic type?

在这种情况下,这些错误消息中的任何一个都没有意义。我可以做些什么来启用泛型类型的位移?

1 个解决方案

#1


7  

I have a blog post on this topic that goes into more detail, but essentially there are three steps:

我有关于这个主题的博客文章更详细,但基本上有三个步骤:

  1. Create a new protocol with the bitshift operators and a constructor from UInt8:

    使用bitshift运算符和UInt8中的构造函数创建一个新协议:

    protocol BitshiftOperationsType {
        func <<(lhs: Self, rhs: Self) -> Self
        func >>(lhs: Self, rhs: Self) -> Self
        init(_ val: UInt8)
    }
    
  2. Declare conformance with an extension on each of the integer types - easy since they already implement everything in BitshiftOperationsType:

    声明与每个整数类型的扩展一致 - 很容易,因为它们已经在BitshiftOperationsType中实现了所有内容:

    extension Int    : BitshiftOperationsType {}
    extension Int8   : BitshiftOperationsType {}
    extension Int16  : BitshiftOperationsType {}
    extension Int32  : BitshiftOperationsType {}
    extension Int64  : BitshiftOperationsType {}
    extension UInt   : BitshiftOperationsType {}
    extension UInt8  : BitshiftOperationsType {}
    extension UInt16 : BitshiftOperationsType {}
    extension UInt32 : BitshiftOperationsType {}
    extension UInt64 : BitshiftOperationsType {}
    
  3. Add a generic constraint so T conforms to your new protocol:

    添加通用约束,以便T符合您的新协议:

    func testBytes<T: IntegerType where T: BitshiftOperationsType>(bytesIn: [UInt8], inout dataOut: T){
        let outputSize = sizeof(T)
        var temp: T = 0
        dataOut = 0
        temp = T(bytesIn[0])
        temp = temp << 1
    }
    

Thanks to Martin R. for the fix for the gross bit I had here before!

感谢马丁R.为我以前在这里的总位数做了修复!

#1


7  

I have a blog post on this topic that goes into more detail, but essentially there are three steps:

我有关于这个主题的博客文章更详细,但基本上有三个步骤:

  1. Create a new protocol with the bitshift operators and a constructor from UInt8:

    使用bitshift运算符和UInt8中的构造函数创建一个新协议:

    protocol BitshiftOperationsType {
        func <<(lhs: Self, rhs: Self) -> Self
        func >>(lhs: Self, rhs: Self) -> Self
        init(_ val: UInt8)
    }
    
  2. Declare conformance with an extension on each of the integer types - easy since they already implement everything in BitshiftOperationsType:

    声明与每个整数类型的扩展一致 - 很容易,因为它们已经在BitshiftOperationsType中实现了所有内容:

    extension Int    : BitshiftOperationsType {}
    extension Int8   : BitshiftOperationsType {}
    extension Int16  : BitshiftOperationsType {}
    extension Int32  : BitshiftOperationsType {}
    extension Int64  : BitshiftOperationsType {}
    extension UInt   : BitshiftOperationsType {}
    extension UInt8  : BitshiftOperationsType {}
    extension UInt16 : BitshiftOperationsType {}
    extension UInt32 : BitshiftOperationsType {}
    extension UInt64 : BitshiftOperationsType {}
    
  3. Add a generic constraint so T conforms to your new protocol:

    添加通用约束,以便T符合您的新协议:

    func testBytes<T: IntegerType where T: BitshiftOperationsType>(bytesIn: [UInt8], inout dataOut: T){
        let outputSize = sizeof(T)
        var temp: T = 0
        dataOut = 0
        temp = T(bytesIn[0])
        temp = temp << 1
    }
    

Thanks to Martin R. for the fix for the gross bit I had here before!

感谢马丁R.为我以前在这里的总位数做了修复!