2

I am new to programming and am currently studying a simple game file written in swift provided by Jeoy Devilla.

One thing I can't understand is why he assigns UInt32 values to category collisions:

// Physics body category bitmasks
// ------------------------------
// We'll use these to determine missle-alien collisions
private let missileCategory: UInt32 = 0x1 << 0   // 00000000000000000000000000000001 in binary
private let alienCategory: UInt32   = 0x1 << 1   // 00000000000000000000000000000010 in binary

I'm trying to figure out why "UInt32 - 0x1 << 0...is used in this situation, and examples of other situations that it is used in. Do people do this anytime they want to assign a variable some arbitrary number? Do they do this to ensure this value will never be accidentally be repeated by another object?

link to Game code: http://www.globalnerdy.com/2014/08/10/a-simple-shoot-em-up-game-with-sprite-kit-and-swift-part-one-last-things-first-the-complete-project/

Thanks!

Tyler Brown
  • 417
  • 1
  • 7
  • 12
  • 2
    That is called an option set or bit flags or a bitmask or some variation thereof. [This piece from Big Nerd Ranch](http://www.bignerdranch.com/blog/smooth-bitwise-operator/) is a great overview. In Swift they're implemented as structs that implement `RawOptionSetType`, which you can [learn more about here on SO](http://stackoverflow.com/questions/24066170/swift-ns-options-style-bitmask-enumerations/24066171#24066171). – Nate Cook Aug 13 '14 at 01:00
  • Thanks so much! Really interesting article! – Tyler Brown Aug 13 '14 at 01:53

0 Answers0