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.
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.
Re: Question regarding file pointers
x + (N - 1) & ~(N - 1) is the general way to ceil a number.