assign to B.
B \= expression
Integer divide B by
expression and assign to B.
Table 5.3: Shortcut Arithmetic Operators
Using these operators will cut down on the typing you have to do, especially for
statements such as a=a+1, which can be written as a+=1.
Binary Number System
Computers use the binary, or base 2, numbering system to represent data. Base 2
digits are the numbers 0 and 1. A single binary 1 or 0 is called a bit. Four bits is called a
nybble. Two nybbles, or 8 bits is called a byte and 2 bytes make up a word. The size of a
data type determines how many bits are available to represent a particular number. A
byte has 8 bits, a short has 16 bits, an integer has 32 bits and a longint has 64 bits.
You will notice that each data type is double the size of the previous data type.
This is because the binary system uses powers of 2 to represent data. 2
is 1. 2
is 2. 2
is 8. 2
is 16, and so on. To find the value of a binary number, you start from the
right, which is position 0 and add the power of twos going left if there is a 1 in the bit
position. If a nybble is 1101, then the right-most position is 2
, the next position left is
skipped since it has a zero, followed by 2
and finally 2
. Resolving the power terms gives
you 1 + 4 + 8 which equals 13. The value ranges for the different data types is a direct
result of the number of bits in each data type.
Being able to manipulate individual bits, bytes and words has a number of uses.
For example, the messaging system of the Windows API use integers to store both the id
of a control and event the control received, as the following code snippet shows.
Listing 5.3: Snippet from Windows Message Loop
In this snippet the id of the control is stored in the low word of wParam, and the
event number is stored in the high word. Since a word is 2 bytes or 16 bits, you can store
65535 ids in a single word, using an unsigned data type, or 32767 ids for a signed data
type. This is a very efficient way to manage data in your program.
The Sign Bit
The sign bit, the leftmost bit, is used by the computer to determine if a signed data
type is negative or positive using the Two's Complement form of notation. To represent a
negative number, the positive value of the number is negated, that is all the 1's are
changed to 0 and the 0's are changed to 1's, and 1 is added to that result. For example,
binary 5 is 0000 0101. Negating all the bits results in 1111 1010. Adding 1 results in 1111
1011. Since the leftmost bit is 1, this is a negative number.
We can confirm this by using the power of 2 notation which results in the following:
) + 64 (2
) + 32 (2
) + 16 (2
) + 8 (2
) + 0 + 2 (2
) + 1 (2
) = - 5. Remember, if
a bit is zero, we add zero to the total. The following program shows the binary
representation of both positive 5 and negative five.
Listing 5.4: signbit.bas
Lines 3 and 4 declare the working variables, a byte that will represent the
actual number and a string that will represent the binary value. In line 6 the myByte is
set to 5. In line 8 the Bin function is used to return a string that represents the binary
value of 5. Since Bin does not return any leading 0's, the String function is used to pad
the string to a full 8 bits for display purposes.
The first parameter of the String function is the number of characters to add and
the second parameter is the character to use to pad the string. Since a byte is 8 bits
long, subtracting the length of myBits from 8 will give the number of 0's to add to the
string, if the length of myBits is less than 8. In line 12 the numeric value of myByte is
printed, which is 5. A semi-colon is added to the end of print statement so that Print will
not print out a carriage return.
In line 13 the binary string is printed in two groups of four, that is each nybble, to
make the display easier to read. The Mid function used in line 13 returns a portion of a
string. The first parameter of the Mid function is the string, the second is the start
position and the third parameter is the number of characters to copy. The first Mid
returns the first four characters, which is appended to a space using the & operator,
which in turn is appended to the last four characters.
Line 15 sets myByte to -5 and lines 17 through 22 format the output and display it
to the screen. The program is ended in the usual manner.
When you run the program the following output is displayed.
Output 5.3: Output of signbit.bas
You can see that the output for -5 matches the Two's Complement form. We can
confirm this by negating 1111 1011 which results in 0000 0100 and adding 1 which
results in 0000 0101, or positive 5.
Why is this important? Signed data types have a smaller range of values than
unsigned data types because a bit is being used to represent the sign of the number. If
you are needing to store a large number of data values, such as ids, in a byte or integer,
the number of possible values you can store depends on whether it is signed or
unsigned. If the number of values needed exceed the range of a signed data type, then
use an unsigned data type.
A Look Ahead
There are times when you need to manipulate individual bits and bytes of a
variable. FreeBasic has a rich set of bitwise operators and macros that you will see in the
C# Image: How to Integrate Web Document and Image Viewer
RasterEdgeImagingDeveloperGuide8.0.pdf: from this user manual, you First, copy the following lines of C# code mode; public string fid; protected void Page_Load copy protection pdf; copy protected pdf to word converter online
FreeBasic includes a number of operators that manipulate the bits of a number.
The following table lists the bitwise operators in order of their precedence. That is, the
first row has the highest precedence while lower rows have lower precedence.
Not (Bitwise negation) B = NOT expression
Inverts operand bit;
turns a 1 into 0 and a 0
B = expression AND
Result bit is 1 only if
both operand bits are 1.
B = expression OR
Result bit is 1 if either
or both operand bits is
B = expression XOR
Result bit is 1 if operand
bits are different.
B = expression EQV
Result bit is 1 if both
operand bits are 0 or 1.
B = expression IMP
Result bit is 0 if first bit
is 1 and second bit is 0,
otherwise result is 1.
Table 6.1: Bitwise Operators
The truth table column indicates the operation on the individual bits. The order of
the bits are not important except for the IMP operator which tests the bits of the second
operand using the bits from the first operand.
The NOT Operator
You saw the NOT operator at work in the two's complement section. The following
program performs the same two's complement operation.
Listing 6.1: not.bas
In line 3 the working variable myByte is declared and initialized using the
alternate format of the Dim statement. Line 6 prints out the value of myByte in both
decimal and binary. In line 8 the NOT operator is used to negate all the bits in the integer.
The result of the operation is printed to the console window in line 10. In line 12, 1 is
added to the result as required by the two's complement method. The result of this
operation id printed in line 14. The program is closed in the usual way.
When the program is run, you should see the following output.
Output 6.1: Output of not.bas
As you can see from the output, the final result of the program is -5 after applying
the twos complement method to myByte. The 1 in the leftmost position indicates that the
number is negative. Bin doesn't add the leading zeros in the first output line, but the
three digits shown are the rightmost three digits.
The AND Operator
The AND operator can be used to test if an individual bit is 1 or 0 by using a mask
value to test for the bit position as the following program illustrates.
Listing 6.2: and.bas
In line 5 the working variables are declared and initialized. The first
section of the program is testing for the third bit position of myByte, which is bit 2
decimal 4. Lines 8 through 10 print out the heading, decimal and binary values for the
variables. The If statement in line 13 uses the AND operator to test for a 1 in the 3
position, and since binary 5 contains a one in this position, the program will execute the
code immediately following the Then keyword.
Line 21 sets the mask value to 2 to test for a 1 in the second bit position, which is
or 2. Line 23 through 25 print out the header, decimal and binary values of the
variables. :ine 28 uses the AND operator to test for a 1, and since binary 5 has a 0 in this
position, the program will execute the code immediately following hr Else keyword.
Running the program produces the following output.
Listing 6.3: Output of and.bas
Looking at the binary values you can see how the bits line up and how the AND
operator can test for individual bits. 5 in binary has a bit set in the 2
(1) and 2
position. Setting the mask value to 4 sets bit position 2
to 1 and all other bit positions to
0. The expression (myByteAndMask) will return an integer value that will contain the
AND values of the two operands. Since the mask has zeros in every position except for
position, all of the other bits will be masked out, that is 0, returning a 4. Since the
return value of 4 matches the target value 4, the code following the Then clause is
The second portion of the program test for the 2
position of myByte. Since this
position contains a 0 in myByte, the value returned from the expression (myByteAnd
Mask) does not match the target value, so the code following the Else clause if executed.
The OR Operator
You can use the OR operator to set multiple values in a single variable. The
Windows API uses this technique to set flags for objects such as the styles of a window.
The following program illustrates this concept.
Listing 6.4: or.bas
Line 4 declares the working variables. In line 7, myByte is set to 2 and in
line 8, that value is combined with 4 using the OR operator. Lines 10 and 11 print out the
decimal and binary values of myByte. In line 14, the mask is set to 2 and in lines 18
through 22 the AND operator is used to test for 2 in myByte. Since myByte contains a 2,
the program will execute the code immediately following the Then clause. Lines 24
through 31 use the same procedure to test for the value of 4. Since myByte contains a 4,
the program will print out the text “myByte contains a 4”. In lines 34 through 41, myByte
is tested for 8, which is does not contains so the code in line 40 will be executed.
When you run the program, you should get the following output.
Documents you may be interested
Documents you may be interested