Professional Documents
Culture Documents
Sign in
home
articles
quick answers
discussions
features
community
help
CPOL
Rate this:
Introduction
I have noticed that some people seem to have problems with bitwise operators, so I decided to write this brief
tutorial on how to use them.
An Introduction to bits
Bits, what are they you may ask?
open in browser PRO version
pdfcrowd.com
Well, simply put, bits are the individual ones and zeros that make up every thing we do with computers. All the data
you use is stored in your computer using bits. A BYTE is made up of eight bits, a WORD is two BYTEs, or sixteen
bits. And a DWORD is two WORDS, or thirty two bits.
Hide Copy Code
0 1 0 0 0 1 1 1 1 0 0 0 0 1 1 1 0 1 1 1 0 1 0 0 0 1 1 1 1 0 0 0
||
|
|
|
||
|+- bit 31
|
|
|
bit 0 -+|
|
|
|
|
|
+-- BYTE 3 -----+--- BYTE 2 ----+--- BYTE 1 ----+-- BYTE 0 -----+
|
|
|
+----------- WORD 1 ------------+----------- WORD 0 ------------+
|
|
+--------------------------- DWORD -----------------------------+
The beauty of having bitwise operators is that you can use a BYTE, WORD or DWORD as a small array or
structure. Using bitwise operators you can check or set the values of individual bits or even a group of bits.
NIBBLE
======
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
HEX VALUE
=========
0
1
2
3
4
5
6
7
8
9
pdfcrowd.com
1001
1010
1011
1100
1101
1110
1111
9
A
B
C
D
E
F
So if we had one BYTE containing the letter 'r' (ASCII code 114) it would look like this:
Hide Copy Code
0111 0010
7
2
binary
hexadecimal
Bitwise operators
There are six bitwise operators. They are:
& The AND operator
| The OR operator
^ The XOR operator
~ The Ones Complement or Inversion operator
>> The Right Shift operator
<< The Left Shift operator.
1
1
0
0
&
&
&
&
1
0
1
0
==
==
==
==
1
0
0
0
An ideal use for this is to set up a mask to check the values of certain bits. Say we have a BYTE that contains some
open in browser PRO version Are you a developer? Try out the HTML to PDF API
pdfcrowd.com
BYTE b =
if ( b &
cout
else
cout
50;
0x10 )
<< "Bit four is set" << endl;
<< "Bit four is clear" << endl;
00110010
& 00010000
---------00010000
- b
- & 0x10
- result
The | operator
The | (OR) operator compares two values, and returns a value that has its bits set if one or the other values, or
both, have their corresponding bits set. The bits are compared using the following table
Hide Copy Code
1
1
0
0
|
|
|
|
1
0
1
0
==
==
==
==
1
1
1
0
An ideal use for this is to ensure that certain bits are set. Say we want to ensure that bit three of some value is set
Hide Copy Code
BYTE b = 50;
BYTE c = b | 0x04;
cout << "c = " << c << endl;
pdfcrowd.com
00110010
| 00000100
---------00110110
- b
- | 0x04
- result
The ^ operator
The ^ (XOR) operator compares two values, and returns a value that has its bits set if one or the other value has its
corresponding bits set, but not both. The bits are compared using the following table
Hide Copy Code
1
1
0
0
^
^
^
^
1
0
1
0
==
==
==
==
0
1
1
0
An ideal use for this is to toggle certain bits. Say we want toggle the bits three and four
Hide Copy Code
BYTE b = 50;
cout << "b = " << b << endl;
b = b ^ 0x18;
cout << "b = " << b << endl;
b = b ^ 0x18;
cout << "b = " << b << endl;
00110010
^ 00011000
---------00101010
- b
- ^ 0x18
00101010
^ 00011000
---------00110010
- b
- ^ 0x18
- result
- result
pdfcrowd.com
The ~ operator
The ~ (Ones Complement or inversion) operator acts only on one value and it inverts it, turning all the ones int
zeros, and all the zeros into ones. An ideal use of this would be to set certain bytes to zero, and ensuring all other
bytes are set to one, regardless of the size of the data. Say we want to set all the bits to one except bits zero and
one
Hide Copy Code
BYTE
cout
WORD
cout
b = ~0x03;
<< "b = " << b << endl;
w = ~0x03;
<< "w = " << w << endl;
00000011
11111100
- 0x03
- ~0x03
0000000000000011
1111111111111100
b
- 0x03
- ~0x03
Another ideal use, is to combine it with the & operator to ensure that certain bits are set to zero. Say we want to
clear bit four
Hide Copy Code
BYTE
cout
BYTE
cout
b = 50;
<< "b = " << b << endl;
c = b & ~0x10;
<< "c = " << c << endl;
00110010
& 11101111
---------00100010
- b
- ~0x10
- result
pdfcrowd.com
BYTE b = 12;
cout << "b = " << b << endl;
BYTE c = b << 2;
cout << "c = " << c << endl;
c = b >> 2;
cout << "c = " << c << endl;
00001100
00110000
00000011
- b
- b << 2
- b >> 2
Bit Fields
Another interesting thing that can be done using bits is to have bit fields. With bit fields you can set up minature
structures within a BYTE, WORD or DWORD. Say, for example, we want to keep track of dates, but we want to use
the least amount of memory as possible. We could declare our structure this way
Hide Copy Code
struct date_struct {
BYTE day
: 5,
month : 4,
year : 14;
} date;
// 1 to 31
// 1 to 12
// 0 to 9999
In this example, the day field takes up the lowest 5 bits, month the next four, and year the next 14 bits. So we can
open in browser PRO version
pdfcrowd.com
store the date structure in twenty three bits, which is contained in three BYTEs. The twenty fourth bit is ignored. If I
had declared it using an integer for each field, the structure would have taken up 12 BYTEs.
Hide Copy Code
|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0|
|
|
|
|
+------ year ---------------+ month +-- day --+
Now lets pick this declaration apart to see what we are doing.
First we will look at the data type we are using for the bit field structure. In this case we used a BYTE. A BYTE is 8
bits, and by using it, the compiler will allocate one BYTE for storage. If however, we use more than 8 bits in our
structure, the compiler will allocate another BYTE, as many BYTEs as it takes to hold our structure. If we had used a
WORD or DWORD, the compiler would have allocated a total of 32 bits to hold our structure.
Now lets look at how the various fields are declared. First we have the variable (day, month, and year), followed by
a colon that separates the variable from the number of bits that it contains. Each bit field is separated by a comma,
and the list is ended with a semicolon.
Now we get to the struct declaration. We put the bit fields into a struct like this so that we can use convention
structure accessing notation to get at the structure members. Also, since we can not get the addresses of bit fields,
we can now use the address of the structure.
Hide Copy Code
date.day = 12;
dateptr = &date;
dateptr->year = 1852;
License
This article, along with any associated source code and files, is licensed under The Code Project Open License
(CPOL)
Share
open in browser PRO version
pdfcrowd.com
pdfcrowd.com
SAPrefs - Netscape-like
Preferences Dialog
Layout Normal
Go
Per page 25
Update
Member 11339123
13-May-15 11:04
ERROR
Atul Khanduri
16-Feb-14 16:27
Nice article....
Atul Khanduri
30-Dec-13 4:14
My vote of 5
Aris79
4-Nov-13 7:30
My vote of 5
fjywade
1-Jun-13 2:21
My vote of 5
Member 9783018
5-Feb-13 11:41
pdfcrowd.com
Useful Topic
duccuongpx
My vote of 4
wangxj_nemo
16-May-12 19:14
My vote of 4
yogesh muchhal
23-Nov-11 18:33
My vote of 5
kamiccolo
My vote of 4
VitStepanek
Member 849910
28-Jul-10 6:05
My vote of 4
thebadboy9999
11-Jul-10 6:12
thank you
Member 3078414
8-May-10 3:00
Roman Tarasov
1-Dec-09 3:33
PJ Arends
4-Dec-09 6:52
22-Jun-12 9:47
1-Jun-11 6:04
1-Aug-10 22:02
useful article
islandscape
14-Oct-09 3:54
is here an error?
langzhengyi
31-May-09 21:53
spacevector
10-Jul-12 22:13
Atul Khanduri
16-Feb-14 16:19
Ravi003
nice thanks
moonbinary
10-Jan-09 19:15
good
AlexanderBX
3-Nov-08 19:58
27-Jan-09 0:10
pdfcrowd.com
cometdlut
adamzhang
News
Question
2-Oct-08 22:38
3-Jul-08 3:25
Refresh
Bug
Answer
Joke
Praise
1 2 3 4 5 Next
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 9 May 2002
Layout: fixed |
Select Language fluid
pdfcrowd.com