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

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>
            性欧美1819sex性高清| 亚洲网址在线| 欧美在线关看| 99ri日韩精品视频| 欧美一区二区成人6969| 欧美日韩国产不卡在线看| 久久国内精品视频| 免费观看成人| 亚洲视频一二区| 欧美高清视频| 欧美成年人网站| 欧美综合激情网| 欧美极品在线播放| 久久美女性网| 亚洲自拍偷拍福利| 国产精品久久久久9999高清| 久久久久国产一区二区三区四区| 免播放器亚洲一区| 日韩视频免费观看高清完整版| 免费看成人av| 91久久极品少妇xxxxⅹ软件| 在线观看亚洲精品视频| 亚洲精品自在久久| 中文久久精品| 欧美日韩在线不卡| 久久国产夜色精品鲁鲁99| 欧美阿v一级看视频| 国产亚洲欧美色| 欧美在线免费观看亚洲| 久久久久久久999精品视频| 欧美色图五月天| 日韩性生活视频| 欧美日韩爆操| 欧美一区二区三区播放老司机 | 欧美精品18+| 亚洲第一成人在线| 久久久天天操| 久久激情五月丁香伊人| 亚洲视频在线观看一区| 99国产精品久久久| 夜夜嗨av一区二区三区| 亚洲日本电影| 国产亚洲福利社区一区| 欧美四级剧情无删版影片| 久久久久五月天| 亚洲国产精品一区二区第四页av| 欧美日韩国产成人| 欧美日韩色一区| 老司机午夜免费精品视频| 午夜欧美理论片| 另类激情亚洲| 日韩天堂av| 国产精品久久久久免费a∨大胸| 一区二区三区四区在线| 蜜臀久久99精品久久久画质超高清| 韩国精品久久久999| 亚洲国产电影| 理论片一区二区在线| 亚洲美女91| 韩日在线一区| 最近中文字幕日韩精品| 亚洲巨乳在线| 国产精品尤物| 欧美日韩视频| 国产老肥熟一区二区三区| 国内在线观看一区二区三区| 日韩手机在线导航| 欧美高清视频一区| 午夜精品视频在线观看一区二区| 一道本一区二区| 久久久久在线观看| 亚洲午夜精品久久久久久浪潮| 欧美刺激午夜性久久久久久久| 欧美日韩另类综合| 日韩亚洲视频在线| 亚洲一区二区三区激情| 欧美成人精品福利| 91久久午夜| 欧美有码视频| 欧美一区二区性| 国产精品草莓在线免费观看| 伊人久久亚洲热| 日韩视频专区| 国产精品另类一区| 亚洲精品一区二区三区婷婷月| 国产亚洲精品福利| 狂野欧美激情性xxxx| 久久国产手机看片| 中日韩午夜理伦电影免费| 亚洲三级视频| 欧美日韩精品免费观看视频完整| 91久久精品一区| 一本一道久久综合狠狠老精东影业| 久久一二三四| 久久久久一区二区三区| 在线观看亚洲精品| 国产视频久久久久| 久久九九久精品国产免费直播| 一本色道久久综合狠狠躁篇的优点| 久久夜色精品国产亚洲aⅴ| 欧美成人免费全部| 亚洲一区二区三区四区五区午夜| 亚洲女性裸体视频| 伊人夜夜躁av伊人久久| 亚洲国产另类久久精品| 欧美日韩一区二区三区在线| 午夜国产精品视频免费体验区| 亚洲精品五月天| 欧美亚洲一区二区在线观看| 一区二区欧美激情| 亚洲视频一区在线观看| 亚洲欧美日韩一区二区三区在线观看| 欧美激情女人20p| 国产精品毛片a∨一区二区三区|国 | 亚洲一区免费观看| 亚洲国产精品久久久久婷婷老年| 国产精品久久久999| 亚洲第一视频| 亚洲欧洲另类| 亚洲欧美另类中文字幕| 欧美成人免费小视频| 亚洲精品乱码久久久久久蜜桃91 | 一区二区日韩伦理片| 在线视频国产日韩| 性娇小13――14欧美| 亚洲欧美日韩在线| 免费高清在线视频一区·| 欧美高清在线| 国内精品久久久久伊人av| 91久久精品国产91性色| 欧美二区视频| 在线观看日韩av| 美女主播一区| 久久五月激情| 欧美诱惑福利视频| 国产精品一区免费视频| 欧美一区二区视频在线| 一区二区三区|亚洲午夜| 欧美三级第一页| 午夜精品视频在线| 亚洲国产视频一区| 91久久综合| 激情一区二区| 午夜久久资源| 伊人成人在线视频| 久久久久久亚洲精品杨幂换脸| 亚洲自拍偷拍色片视频| 蜜桃av综合| 日韩特黄影片| 在线观看亚洲专区| 国模 一区 二区 三区| 欧美日韩精品是欧美日韩精品| 亚洲欧美一区二区原创| 欧美激情欧美激情在线五月| 久久一区二区三区国产精品| 免费亚洲视频| 久久大逼视频| 日韩视频一区二区三区| 午夜精品久久久久久久99水蜜桃 | 欧美一区观看| 91久久久久久久久久久久久| 欧美亚洲一区二区在线| 日韩一级精品| 亚洲综合精品自拍| 亚洲在线一区二区| 中日韩视频在线观看| 9色精品在线| 亚洲一区二区三区在线观看视频 | 欧美日韩高清不卡| 亚洲在线播放电影| 欧美一区二区视频在线| 亚洲欧美在线观看| 亚洲欧美日本日韩| 久久激情一区| 美女视频一区免费观看| 国产精品成人久久久久| 国产精品美女午夜av| 亚洲精品视频免费在线观看| 亚洲美女色禁图| 久久久久国产免费免费| 亚洲在线播放电影| 老司机一区二区| 欧美激情精品久久久久久免费印度 | 亚洲视频1区| 国内偷自视频区视频综合| 欧美精品一区二区精品网 | 欧美激情第1页| 亚洲网站视频福利| 国产精品jvid在线观看蜜臀| 在线看一区二区| 久久久xxx| 99pao成人国产永久免费视频| 午夜精品视频| 欧美ab在线视频| 韩国成人精品a∨在线观看| 亚洲少妇最新在线视频| 亚洲最新视频在线播放| 国产精品99一区| 久久久成人网| 午夜精品久久久久久久蜜桃app|