CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Question regarding file pointers

    I have the following code:

    Code:
    tif->tif_diroff = (TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1) &~ 1;
    What does the above code do? Especially the bit operators

    To give you a fair idea, the code tries to calculate the offset (address) which should be the starting address of the directory information. The directory information is added at the end.

    So it seeks to the end of the file stream and gets the file pointer and then add "+1" and then "&~ 1". I do not understand it. Please help.

    TIFFSeekFile is a function pointer defined as below:

    Code:
    static toff_t
    _tiffSeekProc(thandle_t fd, toff_t off, int whence)
    {
    	DWORD dwMoveMethod, dwMoveHigh;
    
            /* we use this as a special code, so avoid accepting it */
            if( off == 0xFFFFFFFF )
                return 0xFFFFFFFF;
            
    	switch(whence)
    	{
    	case SEEK_SET:
    		dwMoveMethod = FILE_BEGIN;
    		break;
    	case SEEK_CUR:
    		dwMoveMethod = FILE_CURRENT;
    		break;
    	case SEEK_END:
    		dwMoveMethod = FILE_END;
    		break;
    	default:
    		dwMoveMethod = FILE_BEGIN;
    		break;
    	}
            dwMoveHigh = 0;
    	return ((toff_t)SetFilePointer(fd, (LONG) off, (PLONG)&dwMoveHigh,
                                           dwMoveMethod));
    }
    Thanks in advance.
    If there is no love sun won't shine

  2. #2
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Question regarding file pointers

    The result of
    Code:
    TIFFSeekFile(tif, (toff_t) 0, SEEK_END)+1
    is first obtained, and that result is logically ANDed with ~1.

    Since we're considering a 32-bit architecture here, the value 1 as a series of bits would be
    Code:
    0000 0000 0000 0000 0000 0000 0000 0001
    Now if you negate that, you have
    Code:
    1111 1111 1111 1111 1111 1111 1111 1110
    This is ANDed with the above result, effectively stripping off the least significant bit.

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  3. #3
    Join Date
    May 2008
    Posts
    300

    Re: Question regarding file pointers

    x + (N - 1) & ~(N - 1) is the general way to ceil a number.
    Nope

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured