TBitsOpenBit (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The purpose of this example is to demonstrate the OpenBit, Size and Bits methods from the TBits class of the Classes unit. This example will test some bits in an array to see if their property is set to True or False.

Code

int _tmain(int argc, _TCHAR* argv[])
{
	TBits* bits;
	int i;

	/* Create a bit set . */
	bits = new TBits();
	
	/* Set the size of the bit set. */
	bits->Size = 10;
	
	/* Fill the set with values -- True or False. */
	for (i=0;i<bits->Size;i++)
	  if (i % 2 == 0) /* is the number odd or even ? */
		bits->Bits[i] = True; else
		bits->Bits[i] = False;
		
	/* Display to the console which bit is set and which is not. */
	for (i=0;i<bits->Size;i++)
	  if (bits->Bits[i] == True)
		printf("Bit %d is set.\n", i); else
		printf("Bit %d is not set.\n", i);
		
	/* Display the index of the first non-True bit from the array. */
	printf("The index of the first set bit is: %d\n", bits->OpenBit());
	
	/* Free the TBits array */
	delete bits;

	return 0;
}

Uses