User talk:Sdw/Journal
From GPWiki
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello. You should also check out bitfields in structs. They may be useful in your case, depending on how you are storing you data in memory. Bitfields allow you to use separate variable names for different bits in a byte (or bytes). Here's a link on the subject: [1]. ie.
struct tile
{
unsigned int x:3;
unsigned int y:3;
unsigned int walkable:1;
};
You should look a little more into them, as I've might've made a mistake.. I don't think I've actually used them before. Also, instead of hex you can use octal, which is easier to remember which bits are set: 0 - 000 1 - 001 2 - 010 3 - 011 4 - 100 5 - 101 6 - 110 7 - 111 A lot easier than remembering hex. Also, if you use linux/unix you may already be familiar with octal when using chmod. ie, 755 - rwxr-xr-x - 111101101 644 - rw-r--r-- - 110100100 To use octal in C/C++, just prefix the number with a zero: 0755, 0644.
Do you know whether these bit fields are part of the C++ standard? They definitely rock, but I've hardly ever seen anyone use them... why is that? --Marijn 04:07, 1 Jan 2005 (EST)
There are many nice C/C++ features that never seem to see the light of day. One of my favorites is the conditional operator: result = A < B ? A : B; is the same as: if ( A < B ) result = A; else result = B; Codehead 07:09, 1 Jan 2005 (EST) Yeah, bitfields are standard. The reason for not seeing them anywhere is quite simple: they have limited uses. I bet if you take a look at some networking code header files you'll see use of bitfields. Bitfields are only useful when you're playing with the data at the binary level, for anything else you should just use a bool. As for the "conditional" operator, it's called the ternary operator :). It can be pretty useful, but it can also make code look messy sometimes, especially when you nest it.
Bitfields are great if you like fat executables. Normally people don't need the space savings these days and will just write the bitpacking code themselves. One suggestion though: Don't use ^ for exponentiation in the entry about bitwise operations, it's really confusing ;) --Me22 18:56, 19 March 2006 (EST) |


