what does this mean?
#define WS_RECURSIVE (1 << 0)
I understand that it will define WS_Recursive (1 << 0)
but w
It's a bit shift. (1 << 1)
is 2
and (1 << 2)
is 4
. (1 << 0)
is 1
, which is rather silly, but at least it's precomputed at compile time.
<<
computes a bitwise shift to the left. Shifting 1 to the left by 0 bits simply leaves the result as 1.
I noticed also where you got your code from that there's also:
#define WS_RECURSIVE (1 << 0)
#define WS_DEFAULT WS_RECURSIVE
#define WS_FOLLOWLINK (1 << 1)
#define WS_DOTFILES (1 << 2)
#define WS_MATCHDIRS (1 << 3)
That is a way of creating bit fields, where you OR (|
) flags together, and AND them (&
) to check if they're set.
The <<
operator shifts the left-hand value left by the (right-hand value) bits. Your example does nothing! 1 shifted 0 bits to the left is still 1. However, 1 << 1
is 2, 1 << 2
is 4, etc. Is WS_RECURSIVE a flag in a bitfield?
<<
is the left shift operator. It is shifting the number 1
to the left 0
bits, which is equivalent to the number 1
.
It is commonly used to create flags, numbers that can be combined together with |
(bit or) and various operations can be applied to them, such as testing whether a flag is set, setting a flag, removing a flag, etc.
The reason that they can be combined together without interfering with each other is that each one is a power of two, and that is the reason for using 1 << x
, because that yields powers of two:
1 << 0 == 20 == 1 == binary 0001
1 << 1 == 21 == 2 == binary 0010
1 << 2 == 22 == 4 == binary 0100
1 << 3 == 23 == 8 == binary 1000
etc
You can read about bit flags here: http://www.codeproject.com/KB/tips/Binary_Guide.aspx
This is a bit shifting to the left. So 1 << 0 is actually 1. It is usually used this way when you want to define some flags, each of them is one bit set, for example:
#define FLAG1 (1 << 0)
#define FLAG2 (1 << 1)
#define FLAG3 (1 << 2)
#define FLAG4 (1 << 3)