0

please someone explain to me some strange Swift syntax, I've tried to google it and checked Apple's docs but haven't find any example of using it. So here is some code from a tutorial I'm trying to follow. let FSBoundaryCategory: UInt32 = 1 << 0

I don't get what is the meaning of <<.

It's definitely not a twice less than or so :) Please, explain, I'm very curious about it :)

pulp
  • 1,768
  • 2
  • 16
  • 24

3 Answers3

6

This is the "bit shifting" operator.

Maybe the apple documentation ( goo.gl/DXVBJD ) is too complicated to start with.

But, the basic idea is that the initial bit (1) representation is : 00000000000000000000000000000001

If you shift it from one bit, using either this syntax 0b10 or this one 0x1 << 1 the bit goes one place to the left : 00000000000000000000000000000010. You can shift it from two place using 0b100 or 0x1 << 2 and so on...

Here is a table I've made for another question, that might help you too : http://goo.gl/7D8EGY

Community
  • 1
  • 1
lchamp
  • 6,592
  • 2
  • 19
  • 27
  • @Ichamp thank you for your response! I'll try to understand where it will be handy to use bit shifting operator, because now I don't get the idea for what reason I should use it. It may sound a little nooby but I'm a very beginner. – pulp Mar 31 '15 at 20:18
  • A common place to use bits like that is for the collision/contact bitmasks in game development. It is handy because you can easily add them for example : ...00001 + ...00100 will give you ...00101 . Regarding the bit shifting operator itself, it's useful here because you can easily write the value (as seen in the other question I've linked above). _It's probably used in other situations, but I haven't got any other right now._ Anyway, glad it helped you :) – lchamp Mar 31 '15 at 20:27
2

It's a left bitwise shift operator. You can read more about it at Apple Docs here

Shamas S
  • 7,507
  • 10
  • 46
  • 58
1

Existing bits are moved to the left by the requested number of places. In this case 0 means it won't change, so it does nothing.

Paul
  • 53
  • 6