C++ hexadecimal macro usage

  • 2020-04-01 21:25:36
  • OfStack

Popular usage: use each bit of binary to represent a state.
So, 001,010,100, that's three states.
You can combine states by or |.
001 | 010 = 011
001 | 010 | 100 = 111

You can get rid of a state by operating with &.
111 & 001 = 110

You can define such macros to be combined into the parameters of a function
# defineP10x001L / / 001
# defineP20x002L / / 010
# defineP30x004L / / 100
VoidFunc (long) {}
Func (P1 | P2);

This is how you can tell if someone is a 1
Since 001 and XXX have only two states 000 or 001
Such as 001 & 100 = 000001 & 101 = 001
VoidFunc (longl) {
If (l&P1){}//001 and xx0= 000. 001 and xx1=001
If (l&P 2) {} / /
}

Below I use seasoning as an example to write a code intuitive description:
 
#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; 
//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<<" The soy sauce "<<endl; 
} 
if(l&TL_CU) 
{ 
cout<<" vinegar "<<endl; 
} 
if(l&TL_LAJIAO) 
{ 
cout<<" chili "<<endl; 
} 
} 
voidmain() 
{ 
cout<<" You need spices: "<<endl; 
TiaoLiao(TL_LAJIAO|TL_TANG); 
system("pause"); 
} 

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201211/2012111211521961.jpg "border = 0 >  
The nice thing about this is that the code is pretty elegant, you can also use enumerations,
But achieving such a flexible combination may not be so easy.
Hexadecimal means binary, and it is easier to determine the value of someone.

How to represent binary, octal, and hexadecimal variables in C++
1, C and C++ do not provide binary number expression methods.
2,C,C++ language, how to express an octal number?
If this number is 876, we can determine that it is not an octal number, because it is impossible to have more than 7 in an octal number. But if the number is 123, 567, or 12345670, it's possible whether it's octal or decimal.
Therefore,C and C++ stipulate that a number must be preceded by a 0 if it is specified as octal. For example, 123 is decimal, but 0123 is octal.

Int0123;
This is how octal Numbers are expressed in C and C++. One exception is the transliteration '\'.
Because C,C++ rules do not allow the use of slashes plus decimal Numbers to represent characters, so:

'? '/ / ASCII value is 63
'\077'// is in base 8? ', 0 can be omitted, because C,C++ rules do not allow the use of slashes plus decimal Numbers to represent characters
'\0x3F'// is hexadecimal '? '

3. C, C++ states that hexadecimal Numbers must begin with 0x
int0x15A
The x is also case insensitive. Note: 0 in 0x is the number 0, not the letter o.
Note:
1) hexadecimal and hexadecimal can only be used to reach unsigned positive integers, if you write in the code: -078, or: -0xf2,C,C++ do not treat it as a negative number.
2)Qt converts decimal integer values to hexadecimal string methods.
Inta = 63;
QStrings = QString: : number (a, 16); / / s = = "3 f"
QStringt = QString: : number (a, 16). The toUpper (); / / t = = "3 f"
3)QString stores hexadecimal values
// outputs the string in hexadecimal format
QStringcmd = 0 x0a;
QDebug () < <" CMD: "< < CMD. ToAscii (.) toHex ();

4)QString is converted to hexadecimal 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 stores hexadecimal values
Staticconstcharmydata [] = {
0 x00 to 0 x00 to 0 x03, 0 x84, 0 x78, 0 x9c, 0 x3b, 0 x76,
Xc3 xec 0, 0 x18, 0, 0 x31, 0 x0a, 0 xf1, 0 XCC, 0 x99,
0 x6d, 0 x5b
};
QByteArraybd = QByteArray: : fromRawData (mydata, sizeof (mydata));
QDebug () < <" Bd. Data: "< < Bd. The data ();
QDebug () < <" Bd. ToHex () : "< < Bd. ToHex (); // output hexadecimal values
5)QChar stores hexadecimal values and prints them
QCharc = 0 x0a;
QByteArrayarray;
Array. Append (c);
QDebug () < < Array. ToHex (); // the result is "0a"
6) char* store hexadecimal, print
X0b x0a charc [] = {0, 0, '\ 0'};
QByteArrayarray (c);
QDebug () < < Array. ToHex (); / / results "0 a0b"
C++ binary number, decimal, hexadecimal conversion function
1, convert hexadecimal string to decimal integer
 
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 to a decimal integer
 
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; 
} 

Convert a decimal integer into a binary string
 
CStringDECToBIN(intidata) 
{ 
CStringtempStr,outStr; 
intiBIN[32];//Stores an array of binary bits
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 in detail
A bit operation is a method of "adding" and "subtracting" basic units of data.

Firstly, a bit unit is 0 or 1, and the hardware representation is the opening and sum of a pulse, which is the most basic unit of hard and soft communication.
01000111100001110111010001111000
| - bit31... Bit0 - |

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

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

| -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- DWORD -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - |
In c + + often need to use bytes, words, double word to manipulate the data, however, use the binary number to display the number is not very convenient, and use the decimal number display, not the whole, so choose to use a hexadecimal number to display the data, because a hexadecimal number each bits at the right moment is four bits, said an 8-bit byte, a hexadecimal number four said, so a byte said with two hexadecimal number. On this basis, the actual image double word pointer is faster than words pointer arithmetic, word pointer faster than byte Pointers

AscII comparison of 8B+ characters:
8 b +
Decimal number: 566643
Hex: 38422 b
Binary: 001110000100001000101011
The advantage of using bit operations is that you can use BYTE,WORD, or DWORD as a small array or structure. A bit operation allows you to check the value of a bit or assign a bit, or to perform an operation on an entire group of bits.
There are six types of bit operators that can be used:
With operation &
| or operation
The xor operation
~ nonoperation (complement)
> > Moves to the right operation
< < Left shift operation
With the operation (&)
Binocular operation. When you put both of them, you get 1, and everything else is 0.
1 &1 = = 1
1 & 0 = = 0
0 and 1 = = 0
0 and 0 = = 0
One use of the and operation is to check whether the specified bit is set (equal to 1). For example, if there is an identity bit in a BYTE, the code is as follows:
BYTEb = 50;
If (b & 0 x10)
Cout< <" Bitfourisset "< < Endl;
The else
Cout< <" Bitfourisclear "< < Endl;
The above code can be expressed as:
00110010 - b
& 00010000 - & 0 x10
----------------------------
00010000 - the result
You can see that the fourth bit is set.
Or operation (|)
Binocular operation. If you have one of the two bits, it's going to be equal to 1. If both of the two bits are zero, the result is zero.
1 | 1 = = 1
1 | 0 = = 1
0 | 1 = = 1
0 | 0 = = 0
Xor operation (^)
Binocular operation. If the two bits are not equal, the result is 1, otherwise it is 0.
1 ^ 1 = = 0
1 ^ 0 = = 1
0 ^ 1 = = 1
0 ^ 0 = = 0
Xor operations can be used for bit value inversion. For example, flip the values of bit 3 and bit 4:
BYTEb = 50;
Cout< <" B = "< < B< < Endl;
B = b ^ 0 x18;
Cout< <" B = "< < B< < Endl;
B = b ^ 0 x18;
Cout< <" B = "< < B< < Endl;
Can be expressed as:
00110010 - b
^ - ^ 0 x18 00011000
----------
00101010 - the result
00101010 - b
^ - ^ 0 x18 00011000
----------
00110010 - the result
The operation (~)
Monocular operation. The bit value is inverted, set 0 as 1, or set 1 as 0. The purpose of non-operation is to clear the position of the finger to 0 and the rest to 1. Nonoperations are independent of numerical size. For example, clear the first and second bits of 0, and the rest of the position 1:
BYTEb = ~ 0 x03;
Cout< <" B = "< < B< < Endl;
WORDw = ~ 0 x03;
Cout< <" W = "< < W< < Endl;
Can be expressed as:
00000011-0 x03
11111100 - ~ 0 x03b
0000000000000011-0 x03
1111111111111100 - ~ 0 x03w
The combination of non-operations and operations ensures that the index is set to zero. If the fourth bit is cleared by 0:
BYTEb = 50;
Cout< <" B = "< < B< < Endl;
BYTEc = b & x10 ~ 0;
Cout< <" C = "< < C< < Endl;
Can be expressed as:
00110010 - b
& 11101111-0 x10 ~
----------
00100010 - the result
Shift operation (> > With < <)
Moves a bit value in one direction by a specified number of bits. Moves to the right > > Operator moves from high to low, left shift < < The operator moves from low to high. Displacement is often used to align permutations of bits (as in MAKEWPARAM,HIWORD,LOWORD macros).
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
A domain (BitField)
One of the interesting things about bit operations is the bit field. A bit field can be used to create minimal data structures using BYTE,WORD, or DWORD. For example, to save date data and minimize memory usage, you can declare a structure like this:
Structdate_struct {
To31 BYTEday: 5, / / 1
The month: 4, / / 1 to12
Year: 14; / / 0 to9999
} the date;
In the structure, date data occupies a minimum of 5 bits, month occupies 4 bits, and year occupies 14 bits. The entire date data takes only 23 bits, or 3 bytes. Ignore the 24th bit. If the fields are expressed as integers, the entire structure takes 12 bytes.
00000000 | | 00000000 | 00000000 |

+ -- -- -- -- -- -- -- -- -- -- -- -- -- year -- -- -- -- -- -- -- -- -- -- -- -- -- -- + month + - day - a +
Now what happens in this structure declaration
First, take a look at the data types used by bit-domain structures. This is BYTE. A BYTE has 8 bits, and the compiler allocates 1 BYTE of memory. If there is more than 8 bits of data in the structure, the compiler allocates another BYTE until the data requirements are met. If WORD or DWORD is used as the data type for the structure, the compiler allocates a full 32-bit memory to the structure.
Next, look at the domain declaration. The name of a variable (day,month,year) is followed by a colon, followed by the number of digits the variable occupies. Bit fields are separated by commas and terminated by semicolons.
With the use of bit-domain structures, member data can be easily processed as if it were normal structural data. Although we cannot get the address of the bit field, we can use the structure address. Such as:
Date. Day = 12;
Dateptr = & date;
Dateptr - > Year = 1852;

Related articles: