Thread: What does the syntax imply? EXAMPLE: if(VAL << x) & BIT15))

1. What does the syntax imply? EXAMPLE: if(VAL << x) & BIT15))

I do not understand what is happening in this statement.
Code:
```#ifdef ENABLE_TEST_POINTS//tp_write_val(u16);
// Display a value using a test point
void tp_write_val(u16 val)
{
u8 c;

PC7_LOW;
//PC7_HIGH;
nop();
nop();
nop();
PC7_LOW;
for(c=0; c<15; c++)
{
if( (val << c) & BIT15 )
PC7_HIGH;
else
PC7_LOW;
}
PC7_LOW;
//PC7_HIGH;
nop();
nop();
nop();
//PC7_LOW;
}```
What is ((val << c) & BIT15) about? I am sending 15 bits of data to this function and outputting the data serially onto a scope, testing someone else's code to ensue usability (RS-232 may not always be available). I do not understand how this code is comparing each bit as as the loop iterates. BIT15 is defined as 0x8000. PC7_HIGH simply makes test point 34 a high voltage, and PC7_LOW makes it low. I know I am missing something really simple...

2. I'm a bit rusty on bitwise operators, and I don't know what data types "u8" and "u16" are, but the general function would be something like this:

"val" is a value passed to the function.
In the "for()" loop, "val" is shifted over by 'c' bits and logic-ANDed with BIT15.

If we assume "val" = 0x0800, then you can follow it like this:

Code:
```(val << c) & BIT15

// c == 0
// shift val left 0 bit(s)
// val = 0x0800 = 0000 1000 0000 0000
// & BIT15:       1000 0000 0000 0000
//                -------------------
//                0000 0000 0000 0000
// "if()" is false, so output is PC7_LOW

(val << c) & BIT15

// c == 1
// shift val left 1 bit(s)
// val = 0x1000 = 0001 0000 0000 0000
// & BIT15:       1000 0000 0000 0000
//                -------------------
//                0000 0000 0000 0000
// "if()" is false, so output is PC7_LOW

(val << c) & BIT15

// c == 2
// shift val left 2 bit(s)
// val = 0x2000 = 0010 0000 0000 0000
// & BIT15:       1000 0000 0000 0000
//                -------------------
//                0000 0000 0000 0000
// "if()" is false, so output is PC7_LOW

(val << c) & BIT15

// c == 3
// shift val left 3 bit(s)
// val = 0x4000 = 0100 0000 0000 0000
// & BIT15:       1000 0000 0000 0000
//                -------------------
//                0000 0000 0000 0000
// "if()" is false, so output is PC7_LOW

(val << c) & BIT15

// c == 4
// shift val left 4 bit(s)
// val = 0x8000 = 1000 0000 0000 0000
// & BIT15:       1000 0000 0000 0000
//                -------------------
//                1000 0000 0000 0000
// "if()" is true, so output is PC7_HIGH```

3. It's a fair bet (although not certain) that u8 and u16 are 8 and 16 bit unsigned types, respectively.

val << c is bit-shifting val to the left. (val << c ) & BIT15 will yield a non-zero value, if (val << c) and BIT15 have bits set in common.

If we assume that BIT15 is an unsigned value with the 15th bit set, and all other bits not set (for example, the value 1 << 15), then (val << c) & BIT15 determines if the 15th bit of val << c is set.

4. Thanks to all. Makes sense now. u16/u8 is unsigned type, for Raisonance products anyways.