Syntax for C + + hexadecimal macros _c language

Source: Internet
Author: User
Tags bitwise advantage
Popular usage: Each digit of the binary represents a state.
001,010,100 This means three states.
You can combine various states by or | operations.
001|010=011
001|010|100=111

A state can be removed by the operation of the &.
111&001=110

You can define a combination of such macros as a function parameter
#defineP10x001L//001
#defineP20x002L//010
#defineP30x004L//100
Voidfunc (long) {}
Func (p1| P2);

You can tell if a bit is 1.
Since 001 and XXX have only two states of 000 or 001
Like 001&100=000,001&101=001.
Voidfunc (LONGL) {
if (L&P1) {}//001 and xx0=000,001 and xx1=001
if (L&P2) {}//
}

Here I use seasoning as an example to write a paragraph code visual description:
Copy Code code as follows:

#include <iostream>
#include <cstdlib>
USINGNAMESPACESTD;
#defineTL_YAN0x001L//00001 Salt
#defineTL_TANG0x002L//00010 sugar
#defineTL_JIANGYOU0x004L//00100 soy sauce
#defineTL_CU0x008L//01000 vinegar
#defineTL_LAJIAO0x010L//10000 Chili
Typedeflonglong;
Spices
Voidtiaoliao (LONGL)
{
if (L&tl_yan)//00001&xxxx1=00001
{
cout<< "Salt" <<endl;
}
if (L&tl_tang)//00010&xxx0x=00000
{
cout<< "Sugar" <<endl;
}
if (l&tl_jiangyou)
{
cout<< "soy sauce" <<endl;
}
if (L&TL_CU)
{
cout<< "Vinegar" <<endl;
}
if (L&tl_lajiao)
{
cout<< "Chili" <<endl;
}
}
Voidmain ()
{
cout<< "You need seasoning:" <<endl;
Tiaoliao (tl_lajiao| Tl_tang);
System ("pause");
}


The advantage of this is that the code is more elegant, and you can also use enumerations,
But it doesn't seem so easy to achieve such a flexible combination.
hexadecimal represents the binary, and it is easier to determine the value of a bit.

How to represent 2 in C + +, 8-in, 16-binary variables
1, C and C + + do not provide binary number of expression methods.
2, c,c++ language, how to express a octal number?
If this number is 876, we can conclude that it is not a octal number, because it is impossible to have more than 7 Arabic numerals in the octal number. But if this number is 123, is 567, or 12345670, then it is octal number or 10 number, it is possible.
So, c,c++ stipulates, a number if you want to indicate that it uses octal, you must precede it with a 0, such as: 123 is decimal, but 0123 means the use of octal.

int0123;
This is the octal number in C, C + + expression method. But one exception is the signifier ' \ '.
Because the c,c++ rule does not allow the use of slashes plus 10 to represent characters, so:

'?' The ASCII value is 63
' \077 '//is a 8-in-system representation '? ', 0 can be omitted because the c,c++ rule does not allow the use of slashes plus 10 digits to represent characters
' \0x3f '//is the 16-in-system expression '? '

3, c,c++ stipulates that the number of 16 must start with 0x
int0x15a
The x is also case-insensitive. (Note: 0 of the 0x is the number 0, not the letter O).
Note:
1) 8 and 16 can only use an unsigned positive integer if you are in the code:-078, or write: -0xf2,c,c++ does not regard it as a negative number.
2 Converts a decimal integer value into a string method of 16 in Qt.
inta=63;
Qstrings=qstring::number (a,16);//s== "3f"
Qstringt=qstring::number (a,16). ToUpper ();//t== "3F"
3) qstring Store 16 value
Output a string in 16 form
qstringcmd=0x0a;
Qdebug () << "cmd:" <<cmd.toascii (). Tohex ();

4) qstring is converted to 16 in accordance with the string surface format
Qstringstr= "FF";
Boolok;
Inthex=str.toint (&ok,16);//hex==255,ok==true0xff
Intdec=str.toint (&ok,10);//dec==0,ok==false
4) Qbytearray Store 16 value
staticconstcharmydata[]={
0x00,0x00,0x03,0x84,0x78,0x9c,0x3b,0x76,
0xec,0x18,0xc3,0x31,0x0a,0xf1,0xcc,0x99,
0x6d,0x5b
};
Qbytearraybd=qbytearray::fromrawdata (mydata,sizeof (MyData));
Qdebug () << "Bd.data:" <<bd.data ();
Qdebug () << "Bd.tohex ():" <<bd.tohex ()//Output 16 value
5) Qchar Store 16 value, print
qcharc=0x0a;
Qbytearrayarray;
Array.append (c);
Qdebug () <<array.tohex ()//The result is "0a"
6) char* Storage 16, printing
charc[]={0x0a,0x0b, ' n '};
Qbytearrayarray (c);
Qdebug () <<array.tohex ()//result "0a0b"
C + + binary number, decimal, hexadecimal transformation function
1. Converts a hexadecimal string to a decimal integer
Copy Code code as follows:

Worddec (CSTRINGSTR)
{
worddecvalue=0;
inti=0;
For (I=0;i<str. GetLength (); i++)
{
if (str[i]>= ' a ' &&str[i]<= ' F ')
{
decvalue*=16;
decvalue+=str[i]-' F ' +15;
}
ElseIf ((str[i]>= ' A ') && (str[i]<= ' F '))
{
decvalue*=16;
decvalue+=str[i]-' F ' +15;
}
ElseIf (str[i]>= ' 0 ' &&str[i]<= ' 9 ')
{
decvalue*=16;
decvalue+=str[i]-' 0 ';
}
}
Returndecvalue;
}

2. Converts a binary string into a decimal integer
Copy Code code as follows:

Wordbintodem (CSTRINGSTR)
{
worddecvalue=0;
inti=0;
For (I=0;i<str. GetLength (); i++)
{
if (str[i]== ' 1 ')
{
Decvalue+=word (POW (2, str. GetLength () -1-i));
}
}
Returndecvalue;
}

3. Converts a decimal integer to a binary string
Copy Code code as follows:

Cstringdectobin (Intidata)
{
CSTRINGTEMPSTR,OUTSTR;
intibin[32];//stores a binary array of bits per bit
inti=0;
while (idata)
{
ibin[i]=idata%2;
IDATA=IDATA/2;
i++;
}
for (intj=i-1;j>=0;j--)
{
Tempstr.format (L "%d", ibin[j]);
OUTSTR=OUTSTR+TEMPSTR;
}
RETURNOUTSTR;
}

C + + bit operation detailed
A bitwise operation is a method of "adding and" and "deducting" the basic unit that represents the data.

First, a bit unit is 0 or 1, and the hardware representation is a fatty flush open and this is the most basic unit of hard and soft communication. What we call a byte (byte) requires 8 bits to represent a word (word) to two bytes, and 16 bits to represent it. A double word (DWORD) to two words, Four bytes, 32 bits to represent.
01000111100001110111010001111000
|-bit31...bit0-|

|-byte3-| | -byte2-| | -byte1-| | -byte0-|

|---------WORD1--------| | --------WORD0----------|

|-----------------------------DWORD-----------------------------|
In C + + often need to use byte, Word, double word to manipulate the data, however, using this binary number to display the number is not very convenient, and the use of decimal display, can not be the whole, so choose to use 16 to display the data, because a 16-digit each bit is exactly 4 bits, one byte 8 bits, A 16-digit 4-bit representation, so one byte is represented by two 16 digits. Accordingly, the two-word pointer is faster than the word pointer in the actual image operation, and the word pointer is faster than the byte pointer.

ASCII comparison of 8b+ characters:
8b+
Decimal: 566643
16 System: 38422B
Binary system: 001110000100001000101011
The advantage of using bitwise operations is that you can use Byte,word or DWORD as a decimal group or structure. Bitwise operations allow you to check the value or assignment of a bit, or to perform an operation on a whole group of bits.
Bitwise operations can be used with six operators:
& and Operation
| or operation
^ XOR or operation
~ Non-operation (complement)
>> Right Shift operation
<< left-shift operation
and Operations (&)
Binocular operation. When two bits are set (equal to 1), the result equals 1, and the other results are equal to 0.
1&1==1
1&0==0
0&1==0
0&0==0
One use of the operation is to check whether the position is positioned (equal to 1). For example, a byte has an identifier, to check whether the 4th bit is placed, the code is as follows:
byteb=50;
if (b&0x10)
cout<< "Bitfourisset" <<endl;
Else
cout<< "Bitfourisclear" <<endl;
The preceding code can be expressed as:
00110010-b
&00010000-&0x10
----------------------------
00010000-result
You can see that the 4th bit is a bit.
or Operation (|)
Binocular operation. Two bits as long as there is a position, the result is equal to 1. All two digits are 0 o'clock and the result is 0.
1|1==1
1|0==1
0|1==1
0|0==0
Xor or operation (^)
Binocular operation. When two bits are unequal, the result is 1, otherwise 0.
1^1==0
1^0==1
0^1==1
0^0==0
The XOR can be used to flip a bit value. For example, flip the value of position 3rd and 4th:
byteb=50;
cout<< "b=" <<b<<endl;
b=b^0x18;
cout<< "b=" <<b<<endl;
b=b^0x18;
cout<< "b=" <<b<<endl;
Can be expressed as:
00110010-b
^00011000-^0x18
----------
00101010-result
00101010-b
^00011000-^0x18
----------
00110010-result
Non-operation (~)
Monocular operation. The bit value is reversed, 0 is 1, or 1 is 0. The purpose of the non operation is to specify a bit of 0, the rest of the position is 1. Non-operation is independent of numeric size. For example, the 1th and 2nd digits are cleared 0, and the remaining position 1:
byteb=~0x03;
cout<< "b=" <<b<<endl;
wordw=~0x03;
cout<< "w=" <<w<<endl;
Can be expressed as:
00000011-0x03
11111100-~0x03b
0000000000000011-0x03
1111111111111100-~0x03w
A combination of operations and operations ensures that the designation is specified as clear 0. If the 4th digit is cleared 0:
byteb=50;
cout<< "b=" <<b<<endl;
bytec=b&~0x10;
cout<< "c=" <<c<<endl;
Can be expressed as:
00110010-b
&11101111-~0x10
----------
00100010-result
Shift Operations (>> and <<)
Moves the bit value in one direction to the specified number of digits. The right shift >> operator moves from high to low, and the left << operator moves from low to high. The permutation is often used to align the bits (such as the Makewparam,hiword,loword macro function).
byteb=12;
cout<< "b=" <<b<<endl;
bytec=b<<2;
cout<< "c=" <<c<<endl;
c=b>>2;
cout<< "c=" <<c<<endl;
Can be expressed as:
00001100-b
00110000-b<<2
00000011-b>>2
Bit field (bitfield)
A meaningful thing in a bit operation is a bit field. You can use a bit field to create a minimized data structure with Byte,word or DWORD. For example, to save date data and minimize memory footprint, you can declare such a structure:
structdate_struct{
Byteday:5,//1to31
Month:4,//1to12
year:14;//0to9999
}date;
In the structure, date data occupies a minimum of 5 digits, the month occupies 4 digits, occupies 14 bits in the year. This allows the entire date data to occupy only 23 digits, or 3 bytes. Ignore the 24th bit. If you use integers to express each field, the entire structure takes up 12 bytes.
|00000000|00000000|00000000|

+-------------Year--------------+month+--day--+
Now look at what's going on in this structure statement.
First look at the data types used by the bit-domain structure. This is used in byte. 1 byte has 8 bits, and the compiler allocates 1 byte of memory. If the data in the structure is more than 8 bits, the compiler allocates 1 bytes until the data requirements are met. If you use Word or DWORD as the data type of the structure, the compiler assigns a complete 32-bit memory to the structure.
Next look at the domain declaration. The variable (day,month,year) name follows a colon, followed by the number of digits the variable occupies. The bit fields are separated by commas, ending with semicolons.
With a bit-domain structure, you can easily work with member data as you do with normal structure data. Although we cannot get the address of the bit domain, we can use the structure address. For example:
date.day=12;
dateptr=&date;
dateptr->year=1852;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.