Thread: Quick question

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Quick question


    Hi fully aware this is a simple question but could someone clarify what this line means.

    Code:
    (((int16_t)buffer[0]) << 8) | buffer[1]
    Thanks
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Bit-wise operation called a left shift.

    Apparently, buffer has been declared as an array of int8_t, which would be 8-bit integer. Apparently, you're wanting to use the first two elements in that array to create a 16-bit integer value in which buffer[0] is the most significant byte (MSB). So you cast buffer[0] as an int16_t, shift it left by 8 bits, and OR in buffer[1].

    If that doesn't make sense, then review bit-wise operators.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0
    thanks I have covered bitwise operators before, but it was a few years ago and I was having a bit of a brain fart :P. Quick question regarding pointers if i have the variables x,y,and z in my main code and I want to change it inside of a function, I should use pointers right? Would this be the correct way to do it?

    Code:
    void main(void)
    {
        int x;
        int y;
        int z;
    
       function(&x,&y,&z)
    
    }
    then should my function be:
    Code:
    void function(int* x,int* y,int* z)
    {
       /*
       Variable manipulation here e.g
        Z=x+y;
     */
     return
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    pointers usage to change their values


    -> x,y,z are integers in main function,

    -> function(&x,&y,&z); is function call we usually say call by reference

    -> and the formal arguments are also x,y,z these are integer pointers, not integers, the variable in main and pointer variable in function are different.

    -> and to change the values of x,y,z in main u must change the value contained by the pointer variables x,y,z in function definition.

    -> *z= *x+*y; will add contents of x & y and stores in z.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    40
    Rep Power
    19
    google it

    Example http://www.cs.utah.edu/~germain/PPS/Topics/C_Language/c_functions.html

IMN logo majestic logo threadwatch logo seochat tools logo