1、位操作查看bit值
//位与操作查看某个位上的bit值,MASK为0、1数串,其中只有被检测位为1
if((flag&MASK)==MASK)
bit=1;
2、判断奇数偶数
/* -------------判断整数x是奇数还是偶数------------- */
/* 1、常规做法:看除以2的余数 */
if (0 == x % 2)
偶数
else
奇数
/*2、 使用位运算 */
if (0 == x & 1)
偶数
else
奇数
- 因为x & 1中,x是解释为2进制的10进制数,为1、0数串。对于bit数串,令右边第一位编号为1的位,则从2位起,有 21 、 22 ……因此,从2位起保证x为偶数。因此,奇偶性只由1位决定。因此x&1之后,2位起所有bit位重置为0只有最后一位,若最后一位为0,则 20∗0=0 ,为偶数;若为1,则 20∗1=1 ,为奇数。
3、宏定义函数
//最大值函数
#define MAX(X,Y) ((X)>(Y)?(X):(Y))
//绝对值函数
#define ABS(X) ((X)<0)?-(X):(X))
//符号判别函数,如果X为加号或者减号,则宏返回值为1,即真;否则为0,即假。
#define ISSIGN(X) ((X)=='+'||(X)=='-'?1:0)
4,声明定义保留n位小数的float/double型
说明一下,这个不是用 %.2f,之类的,因为这个只能用于printf,scanf而不是真正的把该数储存在一个变量里。
有小技巧:
float a=1.234567;
a=( (float)( (int)( (a+0.005)*100 ) ) )/100;
//保留2位小数的
a=( (float)( (int)( (a+0.0005)*1000 ) ) )/1000;
//保留3位小数的
a=( (float)( (int)( (a+0.00005)*10000 ) ) )/10000;
//保留4位小数的
//即保留n位小数
// a=( (float)( (int)( (a+0. (n个0) 5)*1 (n个0) ) ) )/1 (n个0);
//double型应该类似,未试过。可能只是前面强转为double型
//C++ Operators
// Operators specify an evaluation to be performed on one of the following:
// One operand (unary operator)
// Two operands (binary operator)
// Three operands (ternary operator)
// The C++ language includes all C operators and adds several new operators.
// Table 1.1 lists the operators available in Microsoft C++.
// Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators. Operators associate with either the
//expression on their left or the expression on their right; this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
// Table 1.1 shows the precedence and associativity of C++ operators
// (from highest to lowest precedence).
//
//Table 1.1 C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator | Name or Meaning | Associativity |
//+------------------+-----------------------------------------+---------------+
//| :: | Scope resolution | None |
//| :: | Global | None |
//| [ ] | Array subscript | Left to right |
//| ( ) | Function call | Left to right |
//| ( ) | Conversion | None |
//| . | Member selection (object) | Left to right |
//| -> | Member selection (pointer) | Left to right |
//| ++ | Postfix increment | None |
//| -- | Postfix decrement | None |
//| new | Allocate object | None |
//| delete | Deallocate object | None |
//| delete[ ] | Deallocate object | None |
//| ++ | Prefix increment | None |
//| -- | Prefix decrement | None |
//| * | Dereference | None |
//| & | Address-of | None |
//| + | Unary plus | None |
//| - | Arithmetic negation (unary) | None |
//| ! | Logical NOT | None |
//| ~ | Bitwise complement | None |
//| sizeof | Size of object | None |
//| sizeof ( ) | Size of type | None |
//| typeid( ) | type name | None |
//| (type) | Type cast (conversion) | Right to left |
//| const_cast | Type cast (conversion) | None |
//| dynamic_cast | Type cast (conversion) | None |
//| reinterpret_cast | Type cast (conversion) | None |
//| static_cast | Type cast (conversion) | None |
//| .* | Apply pointer to class member (objects) | Left to right |
//| ->* | Dereference pointer to class member | Left to right |
//| * | Multiplication | Left to right |
//| / | Division | Left to right |
//| % | Remainder (modulus) | Left to right |
//| + | Addition | Left to right |
//| - | Subtraction | Left to right |
//| << | Left shift | Left to right |
//| >> | Right shift | Left to right |
//| < | Less than | Left to right |
//| > | Greater than | Left to right |
//| <= | Less than or equal to | Left to right |
//| >= | Greater than or equal to | Left to right |
//| == | Equality | Left to right |
//| != | Inequality | Left to right |
//| & | Bitwise AND | Left to right |
//| ^ | Bitwise exclusive OR | Left to right |
//| | | Bitwise OR | Left to right |
//| && | Logical AND | Left to right |
//| || | Logical OR | Left to right |
//| e1?e2:e3 | Conditional | Right to left |
//| = | Assignment | Right to left |
//| *= | Multiplication assignment | Right to left |
//| /= | Division assignment | Right to left |
//| %= | Modulus assignment | Right to left |
//| += | Addition assignment | Right to left |
//| -= | Subtraction assignment | Right to left |
//| <<= | Left-shift assignment | Right to left |
//| >>= | Right-shift assignment | Right to left |
//| &= | Bitwise AND assignment | Right to left |
//| |= | Bitwise inclusive OR assignment | Right to left |
//| ^= | Bitwise exclusive OR assignment | Right to left |
//| , | Comma | Left to right |
//+------------------+-----------------------------------------+---------------+