C 程序中5>>2与5<<2有什么区别 C语言中%5.2f中的5和.2都分别代表什么意思?求详细解释...

作者&投稿:智沾 (若有异议请与网页底部的电邮联系)

一、作用不同

1、5>>2:右移运算符,是将一个二进制位的操作数按指定移动的位数向右移动,移出位被丢弃,左边移出的空位或者一律补0,或者补符号位。

2、5<<2:左移运算符,将一个数的各二进制位全部左移若干位,移动的位数由右操作数指定,右操作数必须是非负值,其右边空出的位用0填补,高位左移溢出则舍弃该高位。


二、结果不同

1、5>>2:0000 0101 变成 0000 0001。

2、5<<2:0000 0101 变成 0001 0100。

三、特点不同

1、5>>2:实际移动的次数是移动次数和32的余数,也就是移位33次和移位1次得到的结果相同。

2、5<<2:位移位运算符是同级别的,结合性是自左向右


参考资料来源:百度百科-左移运算符

参考资料来源:百度百科-右移运算符



5>>2是把5的二进制向右移两位,5<<2是把5的二进制向左移两位
5的二进制是00000101
右移两位是 00000001,也就是1
左移两位是 00010100,也就是20

5>>2是指将5的二进制右移2位,即0000 0101 变成 0000 0001
5<<2是指将5的二进制左移2位,即0000 0101 变成 0001 0100

>> 是右移,<<是左移

左移和右移,结果不一样

c语言中 a=5<<2是什么意思~

a=5<<2在C语言中表示将5的二进制数向左移2位,并将左移后的结果赋值给变量a。

详细过程如下:(以8位二进制表示)
5的二进制为:0000 0101
左移2位后变为:0001 0100(末尾补0),对应的十进制数为20,所以a=20

更一般的是,左移x位,相当于乘以2^x,如:
a = x<<y; // 等价于a=x*2^y

%[flags] [width] [.precision] [{h | l | ll | w | I | I32 | I64}] type
% 5 .2 f
最小宽度为5,精度到小数点2位的浮点数格式输出。
详情请看下面:
Format Specification Syntax: printf and wprintf Functions
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at Format Specification Syntax: printf and wprintf Functions.
Describes the syntax for format string arguments to printf, wprintf, and related functions. More secure versions of these functions are available; for more information, see Security Features in the CRT. For information about the individual functions, see the documentation for those specific functions. For a listing of these functions, see Stream I/O.
A format specification, which consists of optional and required fields, has the following form:
%[flags] [width] [.precision] [{h | l | ll | w | I | I32 | I64}] type
Each field of the format specification is a character or a number that signifies a particular format option or conversion specifier. The required type character specifies the kind of conversion to be applied to an argument. The optional flags, width, and precision fields control additional format aspects. A basic format specification contains only the percent sign and a type character—for example, %s, which specifies a string conversion. In the secure versions of the functions, if a percent sign is followed by a character that has no meaning as a format field, the invalid parameter handler is invoked. For more information, see Parameter Validation. In the non-secure versions, the character is copied to the output unchanged. To print a percent-sign character, use %%.
The fields of the format specification control the following aspects of argument conversion and formatting:
typeRequired conversion specifier character that determines whether the associated argument is interpreted as a character, a string, an integer, or a floating-point number. For more information, see printf Type Field Characters.
flagsOptional character or characters that control output justification and output of signs, blanks, leading zeros, decimal points, and octal and hexadecimal prefixes. For more information, see Flag Directives. More than one flag can appear in a format specification, and flags can appear in any order.
widthOptional decimal number that specifies the minimum number of characters that are output. For more information, see printf Width Specification.
precisionOptional decimal number that specifies the maximum number of characters that are printed for strings, the number of significant digits or the number of digits after the decimal-point character for floating-point values, or the minimum number of digits that are printed for integer values. For more information, see "How Precision Values Affect Type" in Precision Specification.
h | l | ll | w | I | I32 | I64Optional prefixes to type that specify the size of the corresponding argument. For more information, see"Size Prefixes" in Size Specification.