function call with "pipe" 
Author Message
 function call with "pipe"

hi, i have come accross a little bit of c code which has me stumped -

er, there's no funciton def, but that's the least of my worries:

unsigned char _i2c_read(void)
{
        register unsigned char dbyte=0;
        register unsigned char numbits=8;
        while(numbits)
        {
                cbi(_i2c_dir,_scl_bit);  // raise scl
                _i2c_delay2();
                dbyte <<=1;              // shift byte left 1
                if ((inp(_i2c_pin) & SDA_MASK)==0) // is sda pin high ?
                        dbyte |= 0x01;                        // yes or
in 1
                sbi(_i2c_dir,_scl_bit);                   // lower scl
                  _i2c_delay2();
                --numbits;
        }
        cbi(_i2c_dir,_sda_bit);                       // raise sda
        _i2c_delay1();
        cbi(_i2c_dir,_scl_bit);                       // raise scl
        _i2c_delay2();
        sbi(_i2c_dir,_scl_bit);                       // lower scl
        return(dbyte);                                // ret byte

Quote:
}

is called with:
        data =((unsigned int) _i2c_read() << 8) & 0xff00;  // read high
8 bits
        data |= (unsigned int) (_i2c_read()&0xff); // read low 4 bits
(high nibble)

what does the << 8 and the & bit do?!? also |= ??

also a function defined as taking input variable unsigned char is
called with

        _i2c_write(A2D_ADDRESS | 0x01);    // write slave address (read)

what does the pipe do??

all help appreciated! i'm completely stumped!!
--

http://www.*-*-*.com/

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Mon, 15 Apr 2002 03:00:00 GMT  
 function call with "pipe"

Quote:

>    data =((unsigned int) _i2c_read() << 8) & 0xff00;  
>    data |= (unsigned int) (_i2c_read()&0xff);
> what does the << 8 and the & bit do?!? also |= ??

> also a function defined as taking input variable unsigned char is
> called with

>    _i2c_write(A2D_ADDRESS | 0x01);  

> what does the pipe do??

Hi Kyle,

- something << n
  means: shift something n-bits left.
  Example:
  int something=0x01;
  something=something<<1; /* will result in 0x02 being in something */

- something & mask
  means: perform a bitwise AND on something with bitmask mask
  Ex:
  int something=0x0f;
  int mask=0x01;
  something=something & mask; /* something is now set to 0x0e */

- something | mask
  means: perform bitwise OR

- |= (and its friends &= += -= *= /= %/ ...)
  a |= b; is an abbreviation for  a= a|b;
  ( a*=10;  is a=a*10; )

Regards Rainer



Mon, 15 Apr 2002 03:00:00 GMT  
 function call with "pipe"

Quote:

>   Ex:
>   int something=0x0f;
>   int mask=0x01;
>   something=something & mask; /* something is now set to 0x0e */

Which is obviously WRONG!! something is 0x01 after this ... sorry ... Regs...Rainer


Mon, 15 Apr 2002 03:00:00 GMT  
 function call with "pipe"

Quote:

> is called with:
>    data =((unsigned int) _i2c_read() << 8) & 0xff00;  // read high
> 8 bits
>    data |= (unsigned int) (_i2c_read()&0xff); // read low 4 bits
> (high nibble)

> what does the << 8 and the & bit do?!? also |= ??

You should probably learn the C basics...

The "<< 8" is a "shift left by 8 bits", the "&" is a bitwise AND
operation (bitmasking).  (Which is most likely not needed at all in
the above expression, assuming _i2c_read() could only return 8-bit
quantities and returns them as an unsigned int already.)

Quote:
>    _i2c_write(A2D_ADDRESS | 0x01);    // write slave address (read)

> what does the pipe do??

It's not a pipe, but a bitwise OR operation.  Likewise, the "|="
combines such an OR operation with an assignment (i. e., it
effectively adds the bits that are set in the expression on the
right-hand side to the variable's value on the left-hand side).

That looks like some direct hardware maninpulation, e. g. like it can
often be found in a device driver in a Unix implementation.

Again, please learn C, operators belong to the very basic things of a
language you ought to understand.

--
Joerg Wunsch             NIC hdl: JW11-RIPE             On the air: DL8DTL
See http://www.interface-business.de/~j/ for more information.

The from address in the headers is wrong (sorry - I'm not the admin here).



Mon, 15 Apr 2002 03:00:00 GMT  
 function call with "pipe"
[...]

Quote:
> data =((unsigned int) _i2c_read() << 8) & 0xff00;  // read high 8 bits
> data |= (unsigned int) (_i2c_read()&0xff); // read low 4 bits (high nibble)
> what does the << 8 and the & bit do?!? also |= ??

You need a book that discusses the C programming language.  It will
explain the meaning and usage of C's basic operators.  A newsgroup is
a very inefficient way to learn these.

The '<<' operator is C's bitwise left shift operator.  Thus, if
_i2c_read() returns 0x12, then _i2c_read() << 8 becomes 0x1200, because
it's shifted left 8 bits.

The '&' operator is C's bitwise AND operator.  The AND operator takes
two inputs, and returns 1 only if both inputs are 1.  Since this is a
bitwise operator, corresponding bits from each input integer are taken
as inputs.  In this case, the & 0xff00 is commonly referred to as a
mask.  For example, if _i2c_read() returns 0x1234, << 8 will yield
0x123400, and & 0xff00 will yield 0x3400.

The '|=' operator is a shorthand, because in C, 'a = a | b' is almost
equivalent to 'a |= b'.  The '|' operator is C's bitwise OR operator,
which is similar to the AND operator above, except that it returns 1
if either or both inputs are 1.

Quote:
> also a function defined as taking input variable unsigned char is
> called with
>    _i2c_write(A2D_ADDRESS | 0x01);    // write slave address (read)
> what does the pipe do??

Logical OR, as explained above.

Quote:
> all help appreciated! i'm completely stumped!!

You need a good book on C.  The comp.lang.c FAQ at

  http://www.eskimo.com/~scs/C-faq/top.html

suggests several.  You are doing yourself a disservice if you think my
explanation (or indeed most anybody's explanation) in a newsgroup would
substitute for a real discussion on these operators.



Mon, 15 Apr 2002 03:00:00 GMT  
 function call with "pipe"
: is called with:
:       data =((unsigned int) _i2c_read() << 8) & 0xff00;  // read high
: 8 bits
:       data |= (unsigned int) (_i2c_read()&0xff); // read low 4 bits
: (high nibble)

: what does the << 8 and the & bit do?!? also |= ??

The '<< ' is called a 'shift' operator and shifts the bit pattern of
the number "to the left" by 8 bits (in the case of '<< 8).  For example,
if a binary (bit) representation of a number is something like this:

0000111010011001  and you shift it to the left 4 bits (<< 4), then the
new number is:
1110100110010000

The '&' is  a bitwise 'and' operator, '|' is a bitwise 'or' and

So, suppose the bit representations of two numbers are:

A = 0001101101110011
B = 1010101010101010

A & B = 0000101000100010
A | B = 1011101111111011

Now the '|=' is very much like the += operator.   The expression

A |= B;  is equivalent to

A = A | B;  

Good Luck,

Paul

--
Paul D. Boyle

North Carolina State University
http://laue.chem.ncsu.edu/web/xray.welcome.html



Mon, 15 Apr 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How to call the function "RasEnumEntries"

2. The mysterious "Call" function

3. what is called a "hush function"?

4. "Pure Virtual Function Call"

5. Passing "callback" function to a function

6. about "call by value" and "call by reference"

7. "c" calling Fortran, and Fortran calling "c"

8. remove() vrs fopen("""w")

9. Displaying binary data as ascii "1"'s and "0"'s

10. Looking for "Shroud"/"Obfus"

11. ""help with TSR""

12. Parse trees and "("")"

 

 
Powered by phpBB® Forum Software