Usage of the C ++ hexadecimal macro

Source: Internet
Author: User
Tags arabic numbers

Popular usage: each bit of binary represents a State.
001,010,100 indicates three states.
Through or | operations, you can combine various States.
001 | 010 = 011
001 | 010 | 100 = 111

You can remove a certain State through and & Operations.
111 & 001 = 110

You can define such macros to combine them into function parameters.
# DefineP10x001L // 001
# DefineP20x002L // 010
# DefineP30x004L/100
VoidFunc (long ){}
Func (P1 | P2 );

You can determine whether a user is 1 in this way.
Because 001 and xxx only have two states: 000 or 001
For example, 001 & 100 = 000,001 & 101 = 001
VoidFunc (longl ){
If (l & P1) {}// 001 and xx0 = 000,001 and xx1 = 001
If (l & P2 ){}//
}

The following section uses spices as an example to describe the Code:Copy codeThe Code is 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 chilies
TypedeflongLONG;
// Seasoning
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 <"chilies" <endl;
}
}
Voidmain ()
{
Cout <"you need spices:" <endl;
TiaoLiao (TL_LAJIAO | TL_TANG );
System ("pause ");
}


The advantage of doing so is that the Code is more elegant, you can also use enumeration,
However, it seems that it is not so easy to implement such a flexible combination.
The hexadecimal notation is binary, which makes it easier to determine a value.

How to represent binary, octal, and hexadecimal variables in C ++
1. Neither C nor C ++ provides the expression of binary numbers.
2. How do I express an octal number in C and C ++?
If the number is 876, we can conclude that it is not an octal number, because the octal number cannot contain more than 7 Arabic numbers. However, if the number is 123, 567, or 12345670, it is possible to set the number to octal or decimal.
Therefore, C, C ++ specifies that if a number is specified to use octal, a 0 value must be added before it. For example, 123 is in decimal format, but 0123 indicates that octal is used.

Int0123;
This is the expression of Octal numbers in C and C ++. But one exception is the escape letter '\'.
Because C and C ++ do not allow the use of a slash plus a 10-digit number to represent characters, so:

'? '// The ASCII value is 63
'\ 077' // is an octal representation '? ', 0 can be omitted, because C, C ++ does not allow the use of a slash plus a 10-digit number to represent characters
'\ 0x3f' // hexadecimal representation '? '

3. C, C ++ requires that the hexadecimal number must start with 0x.
Int0x15A
X is not case sensitive. (Note: 0 in 0x is the number 0, not the letter o ).
Note:
1) The octal and hexadecimal values can only be unsigned positive integers. If you are in the Code:-078, or write:-0xF2, C, C ++ does not regard it as a negative number.
2) In Qt, convert the decimal integer value to a hexadecimal string.
Inta = 63;
QStrings = QString: number (a, 16); // s = "3f"
QStringt = QString: number (a, 16). toUpper (); // t = "3F"
3) QString stores hexadecimal values
// Output the string in hexadecimal format
QStringcmd = 0x0a;
QDebug () <"cmd:" <cmd. toAscii (). toHex ();

4) convert QString to hexadecimal according to 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 stores hexadecimal values
Staticconstcharmydata [] = {
0 x, 0 x, 0 x, 0 x, 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 hexadecimal value
5) QChar stores hexadecimal values and prints
QCharc = 0x0A;
QByteArrayarray;
Array. append (c );
QDebug () <array. toHex (); // The result is "0a"
6) char * stores hexadecimal and prints
Charc [] = {0x0A, 0x0B, '\ 0 '};
QByteArrayarray (c );
QDebug () <array. toHex (); // result "0a0b"
C ++ binary number, decimal, hexadecimal conversion function
1. convert a hexadecimal string to a decimal integer.Copy codeThe Code is 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. convert a binary string to a decimal integer.Copy codeThe Code is as follows: wordbw.dem (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. convert a decimal integer into a binary stringCopy codeThe Code is as follows: CStringDECToBIN (intidata)
{
CStringtempStr, outStr;
IntiBIN [32]; // stores the array of each bit binary
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 operations
Bitwise operations are the methods for "adding" and "Removing" the Basic Units that represent data.

First, a bit unit is 0 or 1, and the hardware indicates that it is a forward and forward. This is the most basic unit of hard soft communication. each byte is represented by eight characters. a word must contain two bytes and 16 characters. a dword requires two characters, four bytes, and 32 characters.
01000111100001110111010001111000
|-Bit31... bit0-|

|-BYTE3-|-BYTE2-|-BYTE1-|-BYTE0-|

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

| ----------------------------- DWORD ----------------------------- |
In C ++, bytes, words, and double words are often used to operate data. However, it is not convenient to use this binary number to display the data, but in decimal number, cannot be rounded up. Therefore, the hexadecimal number is used to display data. Because a hexadecimal number is exactly represented by four binary bits, one byte is 8 bits, A hexadecimal number is represented by four digits, so a byte is represented by two hexadecimal numbers. based on this, the dual-word pointer is faster than the single-word pointer in actual image operations, and the single-word pointer is faster than the byte pointer.

AscII comparison of 8B + characters:
8B +
Decimal: 566643
Hexadecimal: 38422B
Binary: 001110000100001000101011
The advantage of bitwise operations is that BYTE, WORD, or DWORD can be used as a small array or structure. You can use bitwise operations to check bitwise values or assign values, or perform operations on the entire bitwise group.
Bitwise operations have six operators:
& Operation
| Or operation
^ Exclusive or operation
~ Non-computation (complement)
> Right shift operation
<Left shift operation
And operations (&)
Binary operations. When both the two locations are set to 1, the result is equal to 1, and other results are equal to 0.
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
One purpose of the operation is to check whether the position is equal to 1 ). For example, if a BYTE contains a flag, check whether the 4th bits are set. The Code is as follows:
BYTEb = 50;
If (B & 0x10)
Cout <"Bitfourisset" <endl;
Else
Cout <"Bitfourisclear" <endl;
The above code can be expressed:
00110010-b
& Amp; 00010000-& 0x10
----------------------------
00010000-result
We can see that 4th bits are set.
Or (|)
Binary operations. If one of the two locations is set to one, the result is equal to 1. If both of them are 0, the result is 0.
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
XOR (^)
Binary operations. If two digits are not equal, the result is 1; otherwise, the result is 0.
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
The exclusive or operation can be used to flip bitwise values. For example, flip the values of 3rd and 4th bits:
BYTEb = 50;
Cout <"B =" <B <endl;
B = B ^ 0x18;
Cout <"B =" <B <endl;
B = B ^ 0x18;
Cout <"B =" <B <endl;
The table can be:
00110010-b
^ 00011000-^ 0x18
----------
00101010-result
00101010-b
^ 00011000-^ 0x18
----------
00110010-result
Non-computation (~)
Single Object operation. The bitwise value is reversed. Set 0 to 1 or 1 to 0. The purpose of Non-computation is to clear 0 and 1 for the rest. Non-calculation is not related to the value size. For example, clear the 1st and 2nd bits to 0, and the remaining positions are 1:
BYTEb = ~ 0x03;
Cout <"B =" <B <endl;
WORDw = ~ 0x03;
Cout <"w =" <w <endl;
The table can be:
20170011-0x03
11111100 -~ 0x03b
20171000000000011-0x03
1111111111111100 -~ 0x03 w
The combination of non-operation and operation ensures that the value is 0. For example, clear the 4th bits to 0:
BYTEb = 50;
Cout <"B =" <B <endl;
BYTEc = B &~ 0x10;
Cout <"c =" <c <endl;
The table can be:
00110010-b
& Amp; 11101111 -~ 0x10
----------
00100010-result
Shift operation (>>and <)
Moves the bit value to a specified number of digits. Shift right> the operator moves from the high position to the low position, and moves left <The operator moves from the low position to the high position. Usually shift is used to align the positions (such as MAKEWPARAM, HIWORD, and LOWORD macro functions ).
BYTEb = 12;
Cout <"B =" <B <endl;
BYTEc = B <2;
Cout <"c =" <c <endl;
C = B> 2;
Cout <"c =" <c <endl;
The table can be:
20171100-b
001110000-b <2
100000011-b> 2
BitField)
A meaningful thing in BIT operations is bit domain. BYTE, WORD, or DWORD can be used to create a minimal data structure. For example, to save date data and minimize memory usage, you can declare a structure like this:
Structdate_struct {
BYTEday: 5, // 1to31
Month: 4, // 1to12
Year: 14; // 0to9999
} Date;
In the structure, the date data occupies a minimum of 5 digits, the month occupies 4 digits, and the year occupies 14 digits. In this way, the entire date data only takes up to 23 bytes. Ignore the 24th bits. If an integer is used to express each field, the entire structure occupies 12 bytes.
| 00000000 | 00000000 | 00000000 |

+ ------------- Year -------------- + month + -- day -- +
Now let's take a look at what happened in this structure declaration.
First, let's take a look at the data types used by the bit domain structure. Here BYTE is used. One BYTE has eight digits. the compiler will allocate one BYTE of memory. If the data in the structure exceeds 8 bits, the compiler will assign one BYTE until the data requirements are met. If you use WORD or DWORD as the Data Type of the structure, the compiler allocates a complete 32-bit memory to the structure.
Next, let's take a look at the domain declaration. The variable name (day, month, year) follows a colon, followed by the number of digits occupied by the variable. Fields are separated by commas and ended with semicolons.
With the bit domain structure, you can easily process member data as you would with common structured 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;

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.