按位运算的有符号和无符号之间的区别 [英] Difference between signed and unsigned on bitwise operations

查看:417
本文介绍了按位运算的有符号和无符号之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按位运算的有符号变量和无符号变量之间有区别吗?
例如,当处理无符号数字时:
AND 00000111, 00001101
将得到00000101.

但是处理带符号的数字会发生什么?

解决方案

假定2的补码用于有符号数,则关心有符号性的操作(即,对于位串的有符号和无符号解释是不同的)是:

  • 部门
  • 取模
  • 右移
  • 比较(平等除外)
  • 全角乘法(在程序集之外罕见)

与签名无关的操作是:

  • 添加
  • 减法
  • 否定(-x表示~x + 1)
  • 按位和
  • 按位或
  • 按位异或
  • 按位不((~x表示-x - 1)
  • 左移
  • 乘法
  • 比较(仅限平等)

Is there any difference between signed and unsigned variables on bitwise operations?
For example,when dealing with unsigned numbers:
AND 00000111, 00001101
will result 00000101.

But what would happen when dealing with signed numbers?

解决方案

Assuming 2's complement is used for signed numbers, operations that care about signedness (ie they are different for the signed and unsigned interpretation of a bitstring) are:

  • division
  • modulo
  • right shift
  • comparisons (except equality)
  • double-width multiplication (rare outside of assembly)

Operations for which signedness is irrelevant are:

  • addition
  • subtraction
  • negation (-x means ~x + 1)
  • bitwise and
  • bitwise or
  • bitwise xor
  • bitwise not (~x means -x - 1)
  • left shift
  • multiplication
  • comparison (equality only)

这篇关于按位运算的有符号和无符号之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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