Jump to content
  • Advertisement
Sign in to follow this  
OleKaiwalker

Bitwise

This topic is 4612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, Just a quick question for you all. Do the two following lines of code result in the same? 1: assert( (block->prevsize & 0x1) == 0x1 && (block->size & 0x1) == 0x1 ); 2: assert( ((block->prevsize & block->size) & 0x1) == 0x1 ); If so, which of the above do you consider the most elegant and "clean"?

Share this post


Link to post
Share on other sites
Advertisement
Yes, they do evaluate to the same thing. Which you can check using:

assert( ((block->prevsize & 0x1) == 0x1 && (block->size & 0x1) == 0x1 ) == ( ((block->prevsize & block->size) & 0x1) == 0x1) );

The most readable version is:

assert( block->ValidSize( ) );

Block::ValidSize( ) {
return (size & prevsize & 0x1) == 0x1;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Yes, they do evaluate to the same thing. Which you can check using:

assert( ((block->prevsize & 0x1) == 0x1 && (block->size & 0x1) == 0x1 ) == ( ((block->prevsize & block->size) & 0x1) == 0x1) );

The most readable version is:

assert( block->ValidSize( ) );

Block::ValidSize( ) {
return (size & prevsize & 0x1) == 0x1;
}


and mark that method as const so that people feel comfortable using it in an assert:)

Cheers
Chris

Share this post


Link to post
Share on other sites
Or, organized another way around, you could do this:


// Useful for other situations too :/
inline bool is_set(int value, int bit) {
return (value & (1 << bit)) != 0;
}

// now instead of writing a bunch of asserts that call a function,
// we just write a bunch of calls to the function:
inline void Block::debug_checkSize() const {
assert(is_set(size, 0));
assert(is_set(prevsize, 0));
}
// which in release mode should still disappear completely.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!