🤯 1 << channel - 1 in Swift is not 1 << (channel - 1).
⚡ Shifts bind tighter than subtraction.
🔢 Channel 3: (1 << 3) - 1 = 7 → 0b111
💥 Three channels switched on instead of one.
✅ Fix: 1 << (channel - 1) = 4 → only bit 2.
😬 And channel 9? 511 needs 9 bits. UInt8 has 8. Fatal error at runtime.
🧵
🤯 1 << channel - 1 in Swift is not 1 << (channel - 1). ⚡ Shifts bind tighter than subtraction. 🔢 Channel 3: (1 << 3) - 1 = 7 → 0b111 💥 Three channels switched on instead of one. ✅ Fix: 1 << (channel - 1) = 4 → only bit 2. 😬 And channel 9? 511 needs 9 bits. UInt8 has 8. Fatal error at runtime. 🧵
0 views
Published on 24/11/2026 04:29
1:28
🤯 1 << channel - 1 in Swift is not 1 << (channel - 1).
⚡ Shifts bind tighter than subtraction.
🔢 Channel 3: (1 << 3) - 1 = 7 → 0b111
💥 Three channels switched on instead of one.
✅ Fix: 1 << (channel - 1) = 4 → only bit 2.
😬 And channel 9? 511 needs 9 bits. UInt8 has 8. Fatal error at runtime.
🧵
🎯 Rule: bitwise + arithmetic in one expression → parentheses. Always.
#Swift #iOSDev
🎓 Want to learn more? Check Swift Programming for Beginners: Learn Swift from Zero👇
www.udemy.com/course/swift...