按位运算符示例 - 输出是什么? [英] bitwise operator sample - what is the output?

查看:122
本文介绍了按位运算符示例 - 输出是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:



Question :

#include <stdio.h>
int main( ) {
       signed int num = 0xDEADBEE;
}

变量num的符号是什么?

What is the sign of the variable num ?

推荐答案

取决于。



如果你在一个int是16位值的系统中运行,那么你将得到溢出编译错误或负值。

在32位整数系统中(或更高)该值将是正数。



这是关于有符号整数的最重要部分 - 如果它为零,则为正,如果它是负面的。由于你的值只有28位定义,如果可能的32,剩下的4位将为零,结果为正数。



大多数现代编译器(嵌入式工作除外)为32位,专业应用程序偶尔为64位或更高。嵌入式编译器(或交叉编译器)可能是16位。



亲爱的先生,如果签名的数字是0x16,那么它的二进制形式是0001 0110 msb为0 - >正数。

为负签名数,如何找到并找到该msb?例如:两种情况下的0xA和0xABDE是否相同?




不完全!

如果值为0x16那么它的二进制形式不是

Depends.

If you are running in a system where int is a 16 bit value, then you will either get an overflow compilation error, or a negative value.
In a 32 bit integer system (or higher) the value would be positive.

It''s all about the most significant bit of signed integers - if it is zero, it''s positive, if one it''s negative. Since your value has only 28 bit positions defined out if a possible 32, the remaining 4 bits will be zero, and the result is a positive number.

Most modern compilers (except for embedded work) are 32 bit, with occasional 64 or higher for specialist applications. Embedded compilers (or cross compilers) may well be 16 bit.

"Dear sir, If the signed num is 0x16 then its binary form is 0001 0110 the msb is 0 --> positive.
for the negative signed num,how to find and locate that msb? eg : 0xA and 0xABDE for both scenarios is same or not?"


Not quite!
If the value is 0x16 then it''s binary form is not
0001 0110

它是

it is

0000 0000 0001 0110

对于16位系统,并且

for a sixteen bit system, and

0000 0000 0000 0000 0000 0000 0001 0110

用于32位系统。 />
MSB的位置根据变量的大小而变化。因此,您需要知道您正在使用的大小数据,以便确定给定值是正还是负。

(这是使用C#和类似.NET语言的一个优点: int 由语言规范定义为 int32 的同义词 - 一个32位有符号整数。)



对于16位系统,只是MSB是0x8000,对于32位系统,它是0x80000000

具有此值的二进制ANDing将告诉你它是积极的还是消极的。

for a thirty-two bit system.
The position of the MSB changes according to the size of the variable it is going into. So you need to know what size data you are working with in order to decide if a given value is positive or negative.
(This is one advantage of working with C# and similar .NET langauges: int is defined by the language specification as a synonym for int32 - a thirty-two bit signed integer.)

For a sixteen bit system, "just the MSB" is 0x8000, for a thirty-two bit system it is 0x80000000
Binary ANDing with this value will tell you if it is positive or negative.


这篇关于按位运算符示例 - 输出是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆