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

This is a discussion on What does the syntax imply? EXAMPLE: if(VAL << x) & BIT15)) within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    21

    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...

    please and thank you!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,287
    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. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    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.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    21
    Thanks to all. Makes sense now. u16/u8 is unsigned type, for Raisonance products anyways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Syntax Versus Pointer Syntax
    By LyTning94 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2011, 09:56 AM
  2. what does a=NULL imply ??
    By tubby123 in forum C Programming
    Replies: 3
    Last Post: 08-16-2011, 10:51 AM
  3. Syntax help.
    By gp364481 in forum C Programming
    Replies: 1
    Last Post: 02-01-2009, 08:32 AM
  4. what must be syntax?
    By shwetha_siddu in forum Tech Board
    Replies: 15
    Last Post: 04-30-2008, 09:35 AM
  5. About syntax and std::everything
    By Cobras2 in forum C++ Programming
    Replies: 13
    Last Post: 06-04-2003, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21