Skip to main content
gamedev.net
🔒 Locked

Bit Operations.

Started by mrmrcoleman 2 replies 250+ views
Original Post
mrmrcoleman
mrmrcoleman
Bonjourno... I have found the following code in an DirectX9 SDK sample and I need to be able to understand it to move on.
LONG m_lVidPitch;   // Video Pitch

m_lVidPitch  = (m_lVidWidth * 3 + 3) & ~(3); // Forcing RGB24

This code sets the pitch of a surface, although I dont understand the syntax. What does the ''& ~(3)'' do? Also does anyone know of any tutorials where I can learn all this bit manipulation stuff for myself? Thank you in advance. Mark Coleman
glassJAw
glassJAw
If I''m not mistaken, the "& ~(3)" will set the last two bits of the variable to 0.
Rick Scott
Rick Scott
I'm not a bit-twiddling guru, but it looks like the "& ~(3)" part is just clearing the lower 2 bits of the result. ~3 means "binary NOT 3" which means set all the bits to on (1) except those in the #3, which is the last 2 bits. So if an integer is 32 bits, 3 looks like this:


00000000000000000000000000000011


and ~3 looks like this:


11111111111111111111111111111100


And & is "binary and" which means that the bits that are both on stay on in the result, but if either one is off, the result is off. Since all the bits of "~3" are on (except the last couple), it will leave all those bits alone, exept forcing the last 2 to be cleared.

For all that, "& ~(3)" just makes sure the last 2 bits are set to 0 in the end.

[edited by - Rick Scott on January 21, 2004 10:22:42 AM]
quasar3d
quasar3d
quote:
Original post by mrmrcoleman
m_lVidPitch = (m_lVidWidth * 3 + 3) & ~(3); // Forcing RGB24



a scanline should always have a length that's a factor of 4 (so it fits in an array of DWORDS)

the number of bytes needed for a scanline is calculated by:
m_lVidWidth * 3; // 3 colours, and one byte per colour, so 3 bytes per pixel

the number we get from that is probably not a factor of 4 tough, so we have to resize it. In binary, any number that's a factor of 4 has the lowest two bits at 0 (this is like any number that's a factor of 100 in decimal has it's lowest two digits at 0). So if we want to make it a factor of 4, we can simple set the lowest 2 bits to 0. The only problem with this is it will result in a lower number than the original, and then the image wouldn't fit any more. We want the smalles factor of 4 that's larger than the number of bytes needed for the pixels. That's why we add 3 before setting the bits to 0.
To set the two lowest bits to 0, we should & it with a number that has all it's bits at 1, except for the ones we want to be 0.3 is the opposite. It has the lowest two bits at 1, and the others at 0. The ~ operator inverts the bits, so ~3 is just all bits set, except for the lowest two.
Now if we "and" the number we have with that, we get a factor of 4

My Site

[edited by - Quasar3D on January 21, 2004 10:33:23 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.