Home
Site Structure

Programming Home
  Basic Home
    GUI & OS Home
    QB Knowledge Base
    Professor Answers
    Codename Surena
    QB Downloads

Write Us

Special Links
QB DirectoryNEW

Codename Surena
Forums
Sepent Basic GUI & OS Home
Register Log In
QB Knowledge Base > How To Questions > Number 4

How to get/set a bit within a binary number?
Sometimes it is necessary to check the state of a bit within a number. Or you may want to set or clear a bit. For these purposes you can use AND and OR bitwise operators. The following, will help you do that:

Suppose you want to check to bit number n of the value stored in LONG INTEGER variable "var". You may AND the value with 2^n. If the result is non-zero the bit is set, otherwise, it is not:

	IF var AND 2 ^ n THEN
		'The bit is set.
	ELSE
		'The bit is clear.
	END IF
		

Remember that the bit are numbered from right to left beginning at 0.

If you want to set bit number n of the value stored in LONG INTEGER variable "var". You may OR the value with 2^n:

	var = var OR 2 ^ n
		

And if you want to clear that bit, simply AND the value with the invert of 2^n:

	var = var OR (NOT 2 ^ n)