gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > ~? What is that ~?!

#4781 - BhaaL - Wed Apr 09, 2003 2:31 pm

In a tutorial on www.thepernproject.com the author (can't remember his name...) used a function to Flip between the FrontBuffer and the BackBuffer.

Here's the function:
Code:
#define REG_DISPCNT 0x4000000
#define BACKBUFFER 0x10
#define FrontBuffer (u16*)0x6000000
#define BackBuffer (u16*)0x600A000 //start of back buffer in Mode 4
u16* VideoBuffer; //This is were we are currently drawing


void Flip(void)
{
        if(REG_DISPCNT & BACKBUFFER) //back buffer is the current buffer so we need to switch it to the font buffer
        {
                REG_DISPCNT &= ~BACKBUFFER; //flip active buffer to front buffer by clearing back buffer bit
                VideoBuffer = BackBuffer; //now we point our drawing buffer to the back buffer
        }

        else //front buffer is active so switch it to backbuffer
        {
                REG_DISPCNT |= BACKBUFFER; //flip active buffer to back buffer by setting back buffer bit
                VideoBuffer = FrontBuffer; //now we point our drawing buffer to the front buffer
        }
}


Could anybody explain the "~" in the line "REG_DISPCNT &= ~BACKBUFFER;" to me?
_________________
Windows XP is the Answer...
How stupid was the Question?!

#4787 - tepples - Wed Apr 09, 2003 7:20 pm

BhaaL wrote:
Could anybody explain the "~" in the line "REG_DISPCNT &= ~BACKBUFFER;" to me?


The ~ (complement) operator inverts all the bits in its operand and is often used with &.
Code:
if A = 0000001001 then
  ~A = 1111110110

REG | B returns REG with bit B set.
REG & ~B returns REG with bit B clear.

To learn more about C logical operators, read this page.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.