Thread: Can someone explain the following macro in detail?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Can someone explain the following macro in detail?

    #define yahoo_put32(buf, data) ( \
    (*((buf)) = (unsigned char)((data)>>24)&0xff), \
    (*((buf)+1) = (unsigned char)((data)>>16)&0xff), \
    (*((buf)+2) = (unsigned char)((data)>>8)&0xff), \
    (*((buf)+3) = (unsigned char)(data)&0xff), \
    4)


    #define yahoo_get32(buf) ((((*(buf) )&0xff)<<24) + \
    (((*((buf)+1))&0xff)<<16) + \
    (((*((buf)+2))&0xff)<< 8) + \
    (((*((buf)+3))&0xff)))

    why is there a 4 at last on the yahoo_put32?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    << !! Posting Code? Read this First !! >>
    Read it, and edit your post for readability.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The first macro unpacks 4 bytes stored in 'data' to 'buf'
    The second one packs those 4 bytes to make the original data
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Quote Originally Posted by Sipher View Post
    The first macro unpacks 4 bytes stored in 'data' to 'buf'
    The second one packs those 4 bytes to make the original data
    on the first one, the unpacking, is the data's most significant 8 bits putting into the buf?

    I thought the most significant 8 bits should be putting into buf+3?

    and why is there's a 4 at the end?

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Sorry, my bad. See below.
    Last edited by GReaper; 02-23-2011 at 01:43 PM.
    Devoted my life to programming...

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by lilzz View Post
    I thought the most significant 8 bits should be putting into buf+3?
    Data are stored in Big Endian. It's a convertion of the web.

    Quote Originally Posted by lilzz View Post
    and why is there's a 4 at the end?
    It's a comma trick. As you see, all four previous statements have a comma at the end instead of a semicolon. This essentially means the the first macro returns 4 when "executed".
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Macro problem
    By TriKri in forum C Programming
    Replies: 6
    Last Post: 05-14-2010, 02:52 AM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  4. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  5. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM