home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
datafiles
/
text
/
c_manual
/
amiga
/
appendices
/
binaryoperators.doc
< prev
next >
Wrap
Text File
|
1995-02-27
|
7KB
|
236 lines
8 BINARY OPERATORS
8.1 INTRODUCTION
One of the advantages with C compared to other high level
languages is that you are allowed to deep down into the
computer and directly modify bits and bytes. Some processes
that are normally very complicated and tideous to do can
sometimes easily be solved by working directly with the
memory. The code will then be much smaller, faster, and
actually also (normally) much easier to understand.
To be able to manipulate single bits can sometimes be very
handy and sometimes even necessary. A "bit" is the smalest
item in the computer and can only be on (1) or off (0). A
"group" of eight bits is usually reffered as one "byte", and
can represent 256 values (2^8=256). A group of sixteen bits
(two bytes) is called one "word". A group of 32 bits (four
bytes or two words) is called one "long", and finally a group
of 64 bits (eight bytes or four words of two long) is called
"double". See appendix E "Data Types" for more information
about these data types.
Usually you are working with the data types mentioned above
(bytes, words and so on), but you can directrly work with
single bits. I will in this chapter try to explain how you
can manipulate single bits, which operators you may use, and
what effect they have.
8.2 OPERATORS
There exist six different binary operators in C:
1. Bitwise AND (&)
2. Bitwise OR (|)
3. Bitwise EXCL OR (^)
4. Bitwise INVERT (~)
5. LEFT SHIFT (<<)
6. RIGHT SHIFT (>>)
I will below give a short description for each operator
together with some examples. When I use the word "on" I mean
the bit is 1, and the word "off" when the bit is 0.
8.2.1 (&) BITWISE AND
The bitwise operator "&" is used to compare each bit in the
first data field with the corresponding bit in the second bit
field. If both bits are on (1 and 1), the result is 1. In all
other cases, one of the bits is on and the other is off, or
both is off, the result is 0. The "truth table" for AND looks
like this:
AND (&)
----------
0 & 0 -> 0
0 & 1 -> 0
1 & 0 -> 0
1 & 1 -> 1
For example: (11101000) & (10111001) -> (10101000)
(11101000)
& (10111001)
------------
(10101000)
This operator is very useful if you whant to turn some bits off,
but keep others unchanged. Say you have the field (10110011),
and you what to make sure all last four bits are off, but the
first four unchanged. What you have to to is to create a "mask"
which would look like this (11110000). If you then use the
binary operator AND on the field you want to change together
with this "mask", the new field will have all last four bits
turned off, but leave the first four unchanged (10110000).
(10110011) The field you want to change
& (11110000) The "mask"
------------
(10110000) Result
In 'C' it could look something like this: (UBYTE is declared
in header file "exec/types.h" and can be replaced with
"unsigned char".)
UBYTE my_flag_field = 0xB3; /* binary: (10110011) */
UBYTE my_mask = 0xF0; /* binary: (11110000) */
UBYTE my_new_flag_field;
my_new_flag_field = my_flag_field & my_mask;
8.2.2 (|) BITWISE OR
The bitwise operator "|" is used to compare each bit in the
first data field with the corresponding bit in the second bit
field. If both bits or one of them are on, the result is 1. It
is only when both bits are off the result will be 0. The "truth
table" for OR looks like this:
OR (|)
----------
0 | 0 -> 0
0 | 1 -> 1
1 | 0 -> 1
1 | 1 -> 1
For example: (11101000) | (10111001) -> (11111001)
(11101000)
| (10111001)
------------
(11111001)
This operator is normally used when you want to add (turn on)
some bits but leave all others unchanged. As you probably
already have seen in several examples in this manual, the OR
operator can be used to add "flags" (bits) to a field.
A "flag" is actually a name which represents one single bit.
For example, the flag "WINDOWCLOSE" is declared in header
file "intuition/intuition.h" to 0x0008 (0000000000001000),
and WINDOWDRAG to 0x0002 (0000000000000010). If you want to
add these to flags you simply put the OR operator between
them.
In 'C' it would look something like this:
WORD my_window_flags;
my_window_flags = WINDOWCLOSE | WINDOWDRAG;
8.2.3 (^) BITWISE EXCLUSIVE OR
The bitwise operator "^" is used to compare each bit in the
first data field with the corresponding bit in the second bit
field. If one bit is on and the other off the result will be 1.
If both bits are on or both bits are off the result will be 0.
The "truth table" for EXCLUSIVE OR looks like this:
EXCL OR (^)
-----------
0 ^ 0 -> 0
0 ^ 1 -> 1
1 ^ 0 -> 1
1 ^ 1 -> 0
For example: (11101000) ^ (10111001) -> (01010001)
(11101000)
| (10111001)
------------
(01010001)
8.2.4 (~) BITWISE INVERT
The bitwise operator "~" is used to invert each bit in the
data field. If the bit was on it will be turned off, and if
the bit was off it will be turned on. The "truth table" for
INVERT looks like this:
INVERT (~)
----------
~ 1 -> 0
~ 0 -> 1
For example: ~(11101000) -> (00010111)
~ (11101000)
------------
(00010111)
8.2.5 (<<) LEFT SHIFT
The operator "<<" is used to move all bits in a field a
specified number of steps to the left. The number of steps is
stated just after the operator. The right part of the field
will be replaced with 0:s.
For example: (11101000) << 2 -> (10100000)
This operator is very useful when you want to multiply a value
with another value which is a power of two. This operation is
very easy for the computer and therefore extremely fast. To
multiply a value with two, move the bits one step to the left.
To multiplay a value with four, move the bits two steps to the
left and so on.
LEFT SHIFT (<<)
--------------------------------
value << 1 same as: value * 2
value << 2 same as: value * 4
value << 3 same as: value * 8
value << 4 same as: value * 16
value << 5 same as: value * 32
and so on...
8.2.6 (>>) RIGHT SHIFT
The operator ">>" is used to move all bits in a field a
specified number of steps to the right. The number of steps is
stated just after the operator. The left part of the field
will be replaced with 0:s.
For example: (11101000) >> 2 -> (00111010)
This operator is very useful when you want to divide a value
with another value which is a power of two. This operation is
very easy for the computer and therefore extremely fast. To
divide a value with two, move the bits one step to the right.
To divide a value with four, move the bits two steps to the
right and so on.
RIGHT SHIFT (>>)
--------------------------------
value >> 1 same as: value / 2
value >> 2 same as: value / 4
value >> 3 same as: value / 8
value >> 4 same as: value / 16
value >> 5 same as: value / 32
and so on...