Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
What does Int << Float mean? :3
extension Int {
subscript(bit bit: Int) -> Int {
guard (0..<Int.bitWidth).contains(bit) else { return 0 }
return (self >> bit) & 1
}
subscript(bit bit: Float) -> Float {
var whole = bit
whole.round(.down)
let fract = bit - whole
return Float(self[bit: Int(whole)]) * (1 - fract)
+ Float(self[bit: Int(whole) + 1]) * fract
}
}
// My TERRI8LE idea
func <<(lhs: Int, rhs: Float) -> [Float] {
return (0..<Int.bitWidth).map { lhs[bit: Float($0) - rhs] }
}
print(1 << 1.3)
// [0.0, 0.70000005, 0.29999995, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
import Foundation
// Erika's idea
func <<(lhs: Int, rhs: Double) -> Double {
return Double(lhs) * pow(2.0, rhs)
}
print(1 << 1.3)
// 2.4622888266898326
extension Int {
subscript(bit bit: Int) -> Int {
guard (0..<Int.bitWidth).contains(bit) else { return 0 }
return (self >> bit) & 1
}
subscript(bit bit: Float) -> Float {
var whole = bit
whole.round(.down)
let fract = bit - whole
return Float(self[bit: Int(whole)]) * (1 - fract)
+ Float(self[bit: Int(whole) + 1]) * fract
}
}
// Erika's good interpretation of what fractional bits means
func <<(lhs: Int, rhs: Float) -> Float {
return (0..<Int.bitWidth).map { Float(1 << $0) * lhs[bit: Float($0) - rhs] }.reduce(0.0, +)
}
print(1 << 1.3)
// 2.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.