Can you do this? (bit ops)

D

Daniel Rudy

Hello Group,

With all the different threads lately about bit operations, I'm
wondering if you can do this:

typedef unsigned long uint32

uint32 eof;
uint32 bmap;
uint32 blocksect;
uint32 sectsize;
uint32 i;


....
blocksect = 4;
sectsize = 512;
bmap = usbdev.fcb[x].inode.bitmap;
....
for (i = 0; i < blocksect; i++)
eof += (((bmap <<= 1 & 0x80000000) != 0) ? 1 : 0) * sectsize;
....




--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
B

Barry

Daniel Rudy said:
Hello Group,

With all the different threads lately about bit operations, I'm
wondering if you can do this:

typedef unsigned long uint32

uint32 eof;
uint32 bmap;
uint32 blocksect;
uint32 sectsize;
uint32 i;


...
blocksect = 4;
sectsize = 512;
bmap = usbdev.fcb[x].inode.bitmap;
...
for (i = 0; i < blocksect; i++)
eof += (((bmap <<= 1 & 0x80000000) != 0) ? 1 : 0) * sectsize;
...
You can do it, but it probably isn't what you want.
This will & 1 with 0x80000000 and then shift bmap the 0 bits
yielded from that &. I am guessing you want:

eof += ((((bmap <<= 1) & 0x80000000) != 0) ? 1 : 0) * sectsize;
 
D

Daniel Rudy

At about the time of 4/4/2007 3:51 AM, Barry stated the following:
Daniel Rudy said:
Hello Group,

With all the different threads lately about bit operations, I'm
wondering if you can do this:

typedef unsigned long uint32

uint32 eof;
uint32 bmap;
uint32 blocksect;
uint32 sectsize;
uint32 i;


...
blocksect = 4;
sectsize = 512;
bmap = usbdev.fcb[x].inode.bitmap;
...
for (i = 0; i < blocksect; i++)
eof += (((bmap <<= 1 & 0x80000000) != 0) ? 1 : 0) * sectsize;
...
You can do it, but it probably isn't what you want.
This will & 1 with 0x80000000 and then shift bmap the 0 bits
yielded from that &. I am guessing you want:

eof += ((((bmap <<= 1) & 0x80000000) != 0) ? 1 : 0) * sectsize;

Good eye.

Here's the entire function that has that line in it...

/* computes the file EOF from the inode data */
static uint32 fs_inode_ceof(int fd)
{
uint32 i; /* generic counter */
uint32 eof; /* end of file pointer (file size in bytes) */
uint32 blocksize; /* size of disk block (in bytes) */
bmap_t bmap; /* sector bitmap (eof block fragment map) */

eof = 0;
blocksize = usbdev.sect_block * usbdev.sect_size;
bmap = usbdev.fcb[fd].inode.bitmap;
for (i = 0; i < FS_MAXFRAG; i++)
{
eof += usbdev.fcb[fd].inode.loc.length * blocksize;
}
if (bmap != 0)
{
eof -= blocksize;
for (i = 0; i < usbdev.sect_block; i++)
{
eof += ((((bmap <<= 1) & FS_BITMASK) == 0) ? 0 : 1) *
usbdev.sect_size;
}
eof += usbdev.fcb[fd].inode.mod;
}
return(eof);
}




--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
O

Old Wolf

With all the different threads lately about bit operations, I'm
wondering if you can do this:

for (i = 0; i < blocksect; i++)
eof += (((bmap <<= 1 & 0x80000000) != 0) ? 1 : 0) * sectsize;

I'm guessing you want (bmap <<= 1). Also, get rid of the redundant
use of the ternary operator. You could optionally replace !=0 with !!
which I think is a little clearer in this case. The line would be:

eof += sectsize * (0 != ((bmap <<= 1) & 0x80000000));
or
eof += sectsize * !!((bmap <<= 1) & 0x80000000);

Now, you *can* write it this way, but it would be a lot less
confusing to readers (and to yourself!) if you wrote it this way:

for (i = 0; i < blocksect; ++i, bmap <<= 1)
{
if (bmap & 0x40000000)
eof += sectsize;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top