Jump to content
  • Advertisement
Sign in to follow this  
OleKaiwalker

Bitwise

This topic is 4865 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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!