c中的左移运算符(Left shift operators in c)
我正在学习左移操作符和数字乘以10我正在使用此代码。
long int num=a<<3+a<<1;
所以没有。 第一个与8相乘,然后与2相乘,在加上得到一个* 10,存储在num中。
但它给出了一些奇怪的结果,比如for 5 its 2560, for 6 its 6144.
任何人都可以解释一下这个实施中的错误吗?
I am learning about left shift operators and for multiplying a number with 10 I am using this code.
long int num=a<<3+a<<1;
so that no. a first multiplies with 8 and then with 2 and on adding gets a*10 which is stored in num.
But its giving some strange result like for 5 its 2560, for 6 its 6144.
Can anyone please explain whats wrong in that implementation?
原文:https://stackoverflow.com/questions/21090589
更新时间:2020-10-30 06:10
最满意答案
你正在使用的格式实际上是这样的..
num=a<
通过使用括号之类的两个应用移位运算符来区分一些
num=(a<<3)+(a<<1);
The format you are using actually goes this way..
num=a<
make some difference between the two application of shift operators by using parenthesis like
num=(a<<3)+(a<<1);
2014-01-13
相关问答
左移不会截断数字以适合原始长度。 要获得90 ,请使用: (a<<4) & 0xff
0x59是一个int ,可能在你的平台上有sizeof(int)==4 。 然后它是一个0x00000059 。 将它左移4将得到0x00000590 。 另外,在处理按位运算符时,形成使用unsigned int类型的良好习惯,除非您知道自己在做什么。 他们在右移等情况下有不同的行为。 Left shifts does NOT truncate the number to fit the length of
...
为什么逻辑运算符被忽略? 难道有很好的技术原因吗? 他们没有 你可以做&=或|=或^=如果你想要的。 bool b1 = false;
bool b2 = true;
b1 |= b2; // means b1 = b1 | b2
|| 和&&操作员没有复合形式,因为坦白说,他们有点傻。 在什么情况下你想说 b1 ||= b2;
b1 &&= b2;
如果左手侧没有变化,则不评估右侧? 好像只有少数人真的会使用这个功能,所以为什么要放在? 有关复合操作员的更多信息,请参阅我的严肃的文章: ht
...
根据K&R第2版 ,结果取决于签名值的右移。 维基百科说C / C ++'通常'对符号值实现算术移位。 基本上你需要测试你的编译器或不依赖它。 我的VS2008帮助当前的MS C ++编译器说,他们的编译器进行算术移位。 According to K&R 2nd edition the results are implementation-dependent for right shifts of signed values. Wikipedia says that C/C++ 'usually'
...
位移操作员正是他们的名字所暗示的。 他们移位。 这是一个简短的(或不是简短的)介绍不同的班次操作员。 经营者 >>是算术(或签名)右移运算符。 >>>是逻辑(或无符号)右移操作符。 <
...
你的同事基本上用int来代替bool[32]来试图节省空间。 您显示的代码块与此类似 bool[] FRUIT_LAYERS = new bool[32];
FRUIT_LAYERS[LayerMask.NameToLayer("Apple")] = true;
FRUIT_LAYERS[LayerMask.NameToLayer("Banana")] = true;
你可能想要考虑一个更像这样的模式: [Flags]
enum FruitLayers : int
{
Apple = 1
...
当unsigned int类型为32位宽时, 1U << 32是C和C ++中未定义的行为。 (C11,6.5.7p3)“如果右操作数的值为负或大于或等于提升的左操作数的宽度,则行为未定义” (C ++ 11,5.8p1)“如果右操作数为负数,或者大于或等于升级的左操作数的位长度,则行为未定义。” 1U << 32 is undefined behavior in C and in C++ when type unsigned int is 32-bit wide. (C11, 6.5.7p3)
...
bitshift运算符经常被重载以表示从流中读取值和向流写入。 编辑:稍微更详细一点,C ++可以让你重载,或者改变几乎所有操作符的含义。 <>运算符被选为超载,用于写入/读取数据的源或汇,因为它们看起来像箭头或导管。 除了使用什么算子之外,移位的零通用性是零。 The bitshift operator is frequently overloaded to mean reading values from and writing to streams. Edit: In slightly
...
它正在发挥作用,尽管没有按照你的期望做。 你从来没有告诉Python你想要一个32位的值,所以你得到一个整数; Python支持任意大整数。 >>> ((1<<32) - 1) << 8
1099511627520
>>> 1<<123
10633823966279326983230456482242756608L
您期望的行为是静默地丢弃溢出,C行为是出于历史原因。 然而,即使在C语言中,它也不会在你期望的地方做到。 int可能是16,64或18位宽。 当您需要已知尺寸时,还有更多特定类型,例
...
你正在使用的格式实际上是这样的.. num=a<
通过使用括号之类的两个应用移位运算符来区分一些 num=(a<<3)+(a<<1);
The format you are using actually goes this way.. num=a<
make some difference between the two application of shift operators by using parenthesis like num=(a<<3
...
如果我们将问题分成几个步骤,就会很容易理解。 我将首先解释按位部分,以便达到这个问题的任何人都能明白这一点。 最后,您可以看到对您的问题的提及。 开始: $v = 4;
首先,我们检查该值需要多少位。 echo decbin($v); // 3 bits (100)
然后,我们把它换成两个: echo $v << 2; // the value is now 16. Why?
因为如果我们检查位: echo decbin($v << 2); // 5 bits (10000) -> the
...