I dont understand why the "1<<5" in the following snippet of code, didn't find anything on google
gpio_output_set((1<<5), 0, (1<<5), 0);
Why not use 5? or 32? :)
Thanks for the help
I dont understand why the "1<<5" in the following snippet of code, didn't find anything on google
gpio_output_set((1<<5), 0, (1<<5), 0);
Why not use 5? or 32? :)
Thanks for the help
"Why not use 32?"
Because nobody (including person who wrote the code, one year later) knows what gpio_output_set(32)
means. 32 in this case would be what's known as a "magic number", which is programmer slang for a hard-coded number which just sits there in your code, with no rational explanation why, it just magically gets the job done. It is very bad programming practice.
1<<5
on the other hand, is the industry de facto standard way of saying "bit number 5". The intention of the programmer is clear.
Always strive to write self-documenting code, whenever possible.
since 1<<5 is off-course a bit-wise operation, so it is far different from decimal 5. The operation 1<<5 will give a decimal value of 32.
Value of expression 1<<5
is equal to 32
and equivalent to 2 ^ 5
(in the mathematical notation)
Operator <<
is bitwise left shift operator.
From the C Standard (6.5.7 Bitwise shift operators)
4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros.
I think this record
gpio_output_set((1<<5), 0, (1<<5), 0);
was used instead of
gpio_output_set( 32, 0, 32, 0);
that to show how the value is obtained. But in any case it is a bad idea to use magic numbers like 5.