Binary syntax and semantics of C ++, binary syntax Semantics
Binary syntax
C/C ++ default numbers use decimal, octal uses 0 prefix, hexadecimal uses 0x or 0X prefix, the proposal of the binary constant is rejected (reference to section 6.4.4.1 of the international standard for Program Principles in C Language "A proposal to add binary constants was rejected due to lack of precedent and insufficient utility. "), there is no binary representation. GCC uses the 0b/0B prefix as the extension. In fact, many compilers have this extension, but the standard committee has never adopted it. It was a long time before C ++ 14 was introduced. This binary semantics was introduced earlier in Java 7, and Python 2.6 and later 3 were introduced. To avoid confusion between decimal and octal, Python always uses the letter prefix, case-insensitive, and 0b is binary, 0 o is octal, and ox is hexadecimal. When Python2 rose to Python3, it decisively discarded the original octal semantics (that is, the octal semantics with a 0 prefix in C/C ++/Java ).
Representation Method1. The statements in each row below have the same semantics, but different hexadecimal numbers are used for representation.
int i = 0b101010; // binaryint i = 052; // octalint i = 42; // decimalint i = 0x2a; // hexadecimal
2. Use the strtol/strtoll/strtoq function to convert a string to an integer. The base value is 2, which indicates that the string is converted to a binary number. # Include <stdlib. h> long int strtol (const char * nptr, char ** endptr, int base); long int strtoll (const char * nptr, char ** endptr, int base );
3. You can also use the bitset in STL To Do This. It can be expressed by numbers or other characters, which is more powerful than method 2. In this way, I used it in the bacon password decryption question and mapped the/B character to A number of 0/1.
// unsigned long long constructorstd::bitset<8> (42);// string constructorstd::string bits = "110010";std::bitset<8> binary(bits); // [0,0,1,1,0,0,1,0]// string constructor using custom zero/one digitsstd::string bacon = "ABABB";std::bitset<5> m(bacon, 0, std::string::npos, 'A', 'B'); // [0,1,0,1,1]
4. You can use BOOST_BINARY of the boost library to separate several numbers and write them one by one. It is easy to read when a large number is displayed.
int value1 = BOOST_BINARY( 100 111000 01 1 110 );unsigned long value2 = BOOST_BINARY_UL( 100 001 ); // unsigned longlong long value3 = BOOST_BINARY_LL( 11 000 ); // long long if supported
5. If it is not a project but a short program, you can use the C ++ template to simulate binary semantics.
template<unsigned long long N>struct binary{ enum { value = (N%10) + binary<N/10>::value*2 };};template<>struct binary{ enum { value = 0 };}
Int value = static_cast <int> (binary <101010>: value); in the above Code, enum is used to get the result of the variable during compilation, and no integer type is used. Another problem is that if there is 0 at the beginning of the number, the result may be bad. The entire number is parsed as an octal value instead of a decimal value, which easily leads to bugs. A better solution is that the number often starts with 0 (or is forced to add assertions), and the decimal is changed to octal.
// need to enable C++11 flagtemplate<unsigned long long>struct binary{ constexpr static int value = binary<N/8>::value + N%8;}; template<>struct binary<0>{ constexpr static int value = 0;}
6. Use User-defined literals (User-defined literals). Note that this is a newly added function of C ++ 11. Previously, C ++ 03 built some similar semantics (for example, adding the character "f" after the floating point number indicates declaring a float instead of a double), but the user cannot define it by himself. The new literal modifier (literal modifier) is opened for users in C ++ 11, and the object is constructed by the literal value using the custom modifier. All user-defined nominal values must be postpaid and cannot be prepaid. The new literal modifier (literal modifier) is opened for users in C ++ 11, and the object is constructed by the literal value using the custom modifier. All the backend settings are kept as standard except the underline. Therefore, the user must start with an underscore.
int operator "" _B(int i);static_assert( 101010_B == 42);
Which of the following statements is correct in syntax and semantics ____
The answer is C.
A. It is syntactically correct (may cause compilation warnings ). If it is not a static variable or a global variable, otherwise s is not initialized and is a random value. The value of * s cannot be determined, that is, there is no definite semantics. If it is a static variable or a global variable, s is initialized to 0, and there is no definite semantics for the dereference operation on address 0.
B. Error. The reason is the same as above. Operations on random addresses do not have definite semantics (s [0] is equivalent to * s ). In addition, the write operation (Value assignment) on random address units causes unpredictable consequences.
C. Correct. Assign the address of a [1] to pointer s.
D. syntax error. The types of c (int) and a (const int *) do not match, causing compilation errors.
----
[Original reply Group]
References: original
What are the lexical, syntax, and semantics in computer programs?
Lexical, for example, what types of words can be used as variable names:
The variable name must start with a letter, a letter, and a number. The variable name must start with a letter "_",
C language: case sensitive. The maximum length of a name. Reserved Words cannot be used as variable names.
Some languages are case-insensitive. Some names have restrictions on their names.
Reserved Words: system function name, system constant name, and statement. Example:
Sqrt (), sin (), pow (), NULL, EOF, if, while,
Syntax:
Program structure:
Header file
Global volume statement
Function prototype Declaration
Main program block
Subroutine Block
Subroutine Block
Syntax:
Statement:
Loop statement: for (I = 0; I <10; I ++ ){....};
While (..){...}
Switch statement: switch (..){
Case 1:...; break;
Case 2:...; break;
Default:...; break;
}
Condition Statement: if (...) {...;} else {...;};
Statement rules.
Semantics:
The description of your computing process is correct. For example:
Snow is white. -- Correct syntax and correct semantics.
Snow is red. -- Correct syntax and incorrect semantics.
Calculate the sum of 1 to 100 and a few:
The syntax is correct and the semantics is correct.
Sum = 0;
For (I = 1; I <= 100; I ++) sum = sum + I;
Printf ("% d \ n", sum );
Correct syntax and Semantic Error:
Sum = 0;
For (I = 0; I <100; I ++) sum = sum + I;
Printf ("% d \ n", sum );