C语言 C中的左移运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15242788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:35:13  来源:igfitidea点击:

Left shift operator in C

cbit-shift

提问by rvp

Consider:

考虑:

#include <stdio.h>
#define macro(a) a=a<<4;

main()
{
    int a = 0x59;
    printf("%x", a);
    printf("\n");
    macro(a)
    printf("%x", a);
}

For the above code, I am getting the below output:

对于上面的代码,我得到以下输出:

59
590

Why am I not getting the below output as the left shift operation?

为什么我没有得到以下输出作为左移操作?

59
90

回答by phoeagon

Left shifts does NOTtruncate the number to fit the length of the original one. To get 90, use:

左移不会截断数量以适应原来的长度。要获得90,请使用:

(a<<4) & 0xff

0x59is an intand probably on your platform it has sizeof(int)==4. Then it's a 0x00000059. Left shifting it by 4 gives 0x00000590.

0x59是一个int并且可能在您的平台上有sizeof(int)==4。然后是一个0x00000059。左移 4 给出0x00000590

Also, form a good habit of using unsigned inttypes when dealing with bitwise operators, unless you know what you are doing. They have different behaviours in situations like a right shift.

另外,unsigned int在处理按位运算符时要养成使用类型的好习惯,除非您知道自己在做什么。他们在右移等情况下有不同的行为。

回答by rvp

You shifted a hexadecimal number by 4 places to left so you get 590, which is correct.

您将一个十六进制数向左移动 4 位,得到 590,这是正确的。

you had

你有过

000001011001    

shifted to left by 4 bits

左移 4 位

010110010000

is 590 in hex

是 590 十六进制

10010000

is 90 in hex so you might want to remove 0101as is shown by phoeagon

是 90 以十六进制表示,因此您可能想要删除0101,如phoeagon所示

回答by Barath Ravikumar

In your printf if you change %x to %d you get a =89 and after left shifting you will get a = 1424

在您的 printf 中,如果您将 %x 更改为 %d,您将获得一个 =89,左移后您将获得一个 = 1424

Generally for decimal (base 10) numbers

通常用于十进制(基数为 10)数字

a = a<< n  is  a = a*2^n
a = a>> n  is  a = a/2^n

For Hexadecimal (base 16) numbers ,

对于十六进制(基数 16)数字,

Any shift by n (left or right) , can be considered , as a corresponding shift of the digits of the binary equivalent. But this depends on sizeof(int) , used for a given compiler.

任何 n (左或右) 的移位,都可以被认为是二进制等价物的数字的相应移位。但这取决于用于给定编译器的 sizeof(int) 。

回答by Joze

You are using int so you have :

你正在使用 int 所以你有:

 000001011001  

If you shift it by 4 to the left you get :

如果将其向左移动 4,则会得到:

010110010000

If you only want to have only the first 8 bits you don't have to use "int" but unsigned char (or char)

如果您只想拥有前 8 位,则不必使用“int”,而是使用 unsigned char(或 char)

#include<stdio.h>
#define macro(a) a=a<<4;
main()
{
   unsigned char a=0x59;
   printf("%x",a);
   printf("\n");
   macro(a)
   printf("%x",a);            
}

if you still want to use int but only keep the first 8 bits you can use a mask :

如果您仍然想使用 int 但只保留前 8 位,您可以使用掩码:

#define macro(a) a=(a<<4) & 0xFF

回答by qPCR4vir

So could you please tell me what should I do so as to get the output as 0x90? I need to shift the last 4 bits to the first 4 bitsadding 0's at the end

所以你能告诉我我应该怎么做才能得到 0x90 的输出?我需要将最后 4 位移到前 4 位并在末尾添加 0

The only way you can shift the 4 last bit 4 bit to the left AND get it in the place of the first 4 bit is if your type have just 8 bit. Usually this is the case of unsigned char, not int. You will get 0x90for

您可以将最后 4 位 4 位左移并代替前 4 位的唯一方法是您的类型只有 8 位。通常情况下是这种情况 unsigned char,而不是int。您将获得0x90

unsigned char a=0x59;
macro(a)

but when using intthe result is 0x590The error is not with the use of the <<is with the selection of the type. (or a misuse of macro?)

但是当使用int结果是0x590错误不是与使用<<是与类型的选择有关。(或滥用宏?)