1

Here's an example of code in c, i'm not sure what does condition "k & 1" means.

int k,i,c;
k = i >> c;
if (k & 1)
   printf("1");
else
   printf("0");
SSilicon
  • 253
  • 1
  • 2
  • 16
  • 2
    Please, learn to initialize the local variables. before using them. Actually initializing any variable, to some value, is always a good idea, IMHO. if say, `k` is `5`, in `4-bits(nibble)` this will become `0101`. Now performing `AND` operation of this binary number with `1`, like `0101 & 0001` will give, `0001` as a result, since the last bit of `k` is `ON` or k is `odd`. – nIcE cOw Nov 08 '14 at 01:18

1 Answers1

6

k & 1 does a bitwise AND operation of the k variable and the 1 literal. The expression results in either 0 (if the LSB of k is 0) or 1 (if the LSB of k is 1). As the expression is the condition for the if statement, it is implicitly cast to bool, for which 1 maps to true and 0 maps to false.

MooseBoys
  • 6,641
  • 1
  • 19
  • 43