青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

SEMAN

曾經滄海難為水、除卻巫山不是云

C++博客 首頁 新隨筆 聯系 聚合 管理
  9 Posts :: 3 Stories :: 24 Comments :: 0 Trackbacks

Introduction   http://www.codeproject.com/cpp/bitbashing.asp

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?

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.

 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.

Hexadecimal numbers and how they relate to bits

When working with bits, it is kind of hard to express every number using just ones and zeros, which is known as binary notation. To get around this we use hexadecimal (base 16) numbers.

As you may or may not know, it takes four bits to cover all the numbers from zero to fifteen, which also happens to be the range of a single digit hexadecimal number. This group of four bits, or half a BYTE, is called a nibble. As there are two nibbles in a BYTE, we can use two hexadecimal digits to show the value of one BYTE.

NIBBLE   HEX VALUE
======   =========
 0000        0
 0001        1
 0010        2
 0011        3
 0100        4
 0101        5
 0110        6
 0111        7
 1000        8
 1001        9
 1010        A
 1011        B
 1100        C
 1101        D
 1110        E
 1111        F

So if we had one BYTE containing the letter 'r' (ASCII code 114) it would look like this:

0111 0010    binary
  7    2     hexadecimal

We could write it as '0x72'

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.

The & operator

The & (AND) operator compares two values, and returns a value that has its bits set if, and only if, the two values being compared both have their corresponding bits set. The bits are compared using the following table

   1   &   1   ==   1
   1   &   0   ==   0
   0   &   1   ==   0
   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 bit flags, and we want to check if bit four bit is set.

BYTE b = 50;
if ( b & 0x10 )
    cout << "Bit four is set" << endl;
else
    cout << "Bit four is clear" << endl;

This would result in the following calculation

    00110010  - b
 & 00010000  - & 0x10
  ----------
    00010000  - result

So we see that bit four is set.

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

   1   |   1   ==   1
   1   |   0   ==   1
   0   |   1   ==   1
   0   |   0   ==   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

BYTE b = 50;
BYTE c = b | 0x04;
cout << "c = " << c << endl;

This would result in the following calculation

    00110010  - b
  | 00000100  - | 0x04
  ----------
    00110110  - 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

   1   ^   1   ==   0
   1   ^   0   ==   1
   0   ^   1   ==   1
   0   ^   0   ==   0

An ideal use for this is to toggle certain bits. Say we want toggle the bits three and four

BYTE b = 50;
cout << "b = " << b << endl;
b = b ^ 0x18;
cout << "b = " << b << endl;
b = b ^ 0x18;
cout << "b = " << b << endl;

This would result in the following calculations

    00110010  - b
 ^ 00011000  - ^ 0x18
  ----------
    00101010  - result

    00101010  - b
^ 00011000 - ^ 0x18 ---------- 00110010 - result

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

BYTE b = ~0x03;
cout << "b = " << b << endl;
WORD w = ~0x03;
cout << "w = " << w << endl;

This would result in the following calculations

    00000011  - 0x03
    11111100  - ~0x03  b

    0000000000000011  - 0x03
    1111111111111100  - ~0x03  w

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

BYTE b = 50;
cout << "b = " << b << endl;
BYTE c = b & ~0x10;
cout << "c = " << c << endl;

This would result in the following calculations

    00110010  - b
 & 11101111  - ~0x10
  ----------
    00100010  - result

The >> and << operators

The >> (Right shift) and << (left shift) operators move the bits the number of bit positions specified. The >> operator shifts the bits from the high bit to the low bit. The << operator shifts the bits from the low bit to the high bit. One use for these operators is to align the bits for whatever reason (check out the MAKEWPARAM, HIWORD, and LOWORD macros)

BYTE b = 12;
cout << "b = " << b << endl;
BYTE c = b << 2;
cout << "c = " << c << endl;
c = b >> 2;
cout << "c = " << c << endl;

This would result in the following calculations

    00001100  - b
    00110000  - b << 2
    00000011  - 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

struct date_struct {
    BYTE day   : 5,   // 1 to 31
         month : 4,   // 1 to 12
         year  : 14;  // 0 to 9999
    } date;

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 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.

|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.

date.day = 12;

dateptr = &date;
dateptr->year = 1852;


posted on 2005-11-21 08:03 味全每日C++ 閱讀(796) 評論(0)  編輯 收藏 引用
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久深夜福利免费观看| 欧美阿v一级看视频| 久久高清免费观看| 久久超碰97人人做人人爱| 亚洲在线网站| 欧美制服丝袜第一页| 久久精品国产亚洲高清剧情介绍| 久久久久国色av免费观看性色| 狼人社综合社区| 亚洲精品视频二区| 亚洲欧美日韩区| 久久精品亚洲一区二区| 欧美插天视频在线播放| 欧美性大战久久久久久久| 国产欧美日韩精品丝袜高跟鞋| 亚洲欧美日韩一区二区| 激情欧美一区二区三区| 91久久精品网| 亚洲欧美第一页| 美日韩精品视频免费看| 99re6这里只有精品| 久久精品国产91精品亚洲| 欧美成人自拍视频| 国产欧美日本一区二区三区| 亚洲人成在线播放网站岛国| 欧美一区二区视频97| 亚洲黄色在线观看| 亚洲愉拍自拍另类高清精品| 免费看成人av| 国产一区二区在线免费观看 | 久久se精品一区精品二区| 欧美成人免费全部观看天天性色| 亚洲精品在线视频| 久久一区欧美| 国产精品美女久久久久久2018| 在线观看欧美激情| 午夜精品999| 亚洲精品免费一二三区| 久久精品一区二区国产| 欧美无砖砖区免费| 亚洲精品国产系列| 久久夜色精品一区| 亚洲午夜三级在线| 欧美日韩国语| 91久久久久久| 蜜臀av国产精品久久久久| 午夜精品久久久久久久蜜桃app | 久久综合一区二区| 亚洲综合国产| 国产精品mv在线观看| 99国产精品久久| 欧美刺激午夜性久久久久久久| 亚洲一区二区黄色| 欧美日韩免费观看一区=区三区| 亚洲第一色在线| 久久综合五月| 久久激情一区| 国产日韩亚洲欧美精品| 亚洲制服丝袜在线| 一本色道久久综合狠狠躁篇怎么玩 | 亚洲高清资源| 久久国产精品久久精品国产| 国产拍揄自揄精品视频麻豆| 老司机午夜免费精品视频| 日韩视频一区二区| 激情一区二区| 99日韩精品| 黄色一区二区三区四区| 欧美日韩国产在线| 99综合精品| 亚洲免费观看高清在线观看 | 日韩视频精品在线| 欧美激情亚洲自拍| 欧美高清视频一二三区| 99热精品在线| 亚洲老板91色精品久久| 国产精品久久网| 欧美在线免费视屏| 久久精品99国产精品日本| 狠狠色狠狠色综合日日小说| 欧美顶级大胆免费视频| 欧美日韩国产综合一区二区| 午夜国产精品影院在线观看| 久久av一区| 亚洲精品乱码久久久久久蜜桃麻豆| 欧美成人综合| 欧美视频在线一区| 久久精品国产免费看久久精品| 久久精品视频导航| 一区二区不卡在线视频 午夜欧美不卡在 | 亚洲愉拍自拍另类高清精品| 国产亚洲在线观看| 欧美二区在线| 欧美无乱码久久久免费午夜一区| 欧美在线看片a免费观看| 久久综合精品国产一区二区三区| 一本色道**综合亚洲精品蜜桃冫 | 亚洲欧洲中文日韩久久av乱码| 91久久极品少妇xxxxⅹ软件| 国产精品电影网站| 欧美成人小视频| 国产欧美一区二区视频| 亚洲精品中文在线| 在线看无码的免费网站| 正在播放亚洲| 亚洲美女电影在线| 欧美影院成年免费版| 亚洲视频在线看| 麻豆成人综合网| 久久久久久久一区二区三区| 欧美日韩一区二区免费在线观看| 乱码第一页成人| 欧美一区二区三区久久精品茉莉花| 欧美日韩精品一二三区| 亚洲欧美偷拍卡通变态| 久久影视精品| 欧美在线综合| 国产精品久久久久国产a级| 亚洲福利在线观看| 一区二区三区在线视频免费观看 | 欧美网站大全在线观看| 亚洲高清中文字幕| 在线观看精品| 久久精品亚洲精品| 久久琪琪电影院| 国产婷婷97碰碰久久人人蜜臀| 亚洲一区二区三区高清 | 最新国产成人av网站网址麻豆| 久久成人亚洲| 久久久五月婷婷| 国产一区二区三区不卡在线观看| 亚洲欧美国产不卡| 久久精品在线播放| 国产偷自视频区视频一区二区| 亚洲一区二区视频| 欧美专区在线| 精品999在线观看| 久久久久看片| 欧美电影电视剧在线观看| 亚洲国产精品嫩草影院| 欧美.com| 一本色道久久综合亚洲91| 在线亚洲免费视频| 国产精品色一区二区三区| 午夜欧美大尺度福利影院在线看 | 亚洲免费黄色| 欧美激情综合色综合啪啪| 亚洲精选视频在线| 小黄鸭精品aⅴ导航网站入口| 国产精品视频免费观看www| 性亚洲最疯狂xxxx高清| 久久综合九色综合网站| 亚洲精品午夜| 欧美三级在线视频| 午夜精品亚洲| 欧美国产在线视频| 亚洲视频axxx| 国产欧美精品一区| 久久一区二区精品| 99精品国产99久久久久久福利| 欧美亚洲系列| 亚洲欧洲另类国产综合| 欧美日韩一二三区| 欧美影院一区| 亚洲精品日韩一| 欧美一站二站| 亚洲三级视频| 国产拍揄自揄精品视频麻豆| 老司机午夜精品视频| 亚洲天堂av综合网| 欧美成人嫩草网站| 欧美一级电影久久| 亚洲精品一区二区三区蜜桃久| 欧美色视频在线| 久久久久久综合| 久久精品1区| 亚洲精品免费网站| 久久精品国内一区二区三区| 亚洲精品视频一区| 国产婷婷成人久久av免费高清 | 亚洲午夜在线观看视频在线| 老鸭窝91久久精品色噜噜导演| 一区二区三区四区国产| 一区视频在线| 国产精品一级在线| 欧美破处大片在线视频| 久久伊伊香蕉| 午夜精品福利视频| 妖精成人www高清在线观看| 欧美成人首页| 久久女同精品一区二区| 午夜老司机精品| 亚洲午夜精品一区二区| 亚洲啪啪91| 亚洲国产精品免费| 在线成人中文字幕| 狠狠爱www人成狠狠爱综合网| 国产精品久久久一区麻豆最新章节| 欧美国产欧美亚洲国产日韩mv天天看完整 | 国产精品观看|