bit操作和数据表示

二进制和十进制转换

Bits 01010
Value 0*2^4+1*2^3+0*2^2+1*2^1+0*2^0 = 10
Value 102(1100110)
Bits 102/2 = 51…0 ,个位数=0
51/2 = 25…1,十位数=1
25/2 = 12… 1
12/2 = 6… 0
6/2 = 3…0
3/2 = 1…1
1/2 = 0… 1
十进制类比:十进制的也是/10看余数

Group bits as numbers : Three encodings

1.Unsigned encoding

≥0
UMIN=0
UMAX=2^w-1

2.Two’s-complement encoding

最高位是负的
为什么叫Two’s-complement:
7:0111
-7:1000(complement)+1=1001
~x + 1 == -x
TMAX=7
TMIN=-8
[-8,-1] 8个数字
[0,7] 8个数字,跟unsigned表示是一样的
unsigned转2‘s complement

其他signed
Ones’ Complement:

最高位权重=2^(w-1)-1

Sign-Magnitude

最高位是sign位

C 特性
unsigned可以和Two’s complement互转

Type conversion (implicitly)
Type casting (explicitly)
bit不变,重新翻译

short int           x =  12345;
unsigned short int ux = (unsigned short) x;//ux = 12345,不变
short int           y  = -12345;
unsigned short int uy = (unsigned short) y;//uy = 53191,变大正数
By default constants are considered to be signed integers

Unsigned if have “U” as suffix
0U, 4294967259U

If mix unsigned and signed in single expression

signed 变unsigned
operation: <, >, ==, <=, >=
所以 -1 > 0U -1转unsigned

expand

short转long

short int x =  12345;
int ix = (int) x; 
short int y = -12345;
int iy = (int) y;

最高位是0就加0
最高位是1就加1
expand
1001=-7=1-8
11001=-7=1+(8-16)
111001=-7=1+(8+16-32)

truncate

long转short

int       x  = 53191;
short int sx = x;
int       y  = -12345;
Short int sy = y;

truncate
手算十进制结果:
unsigned:mod2^k
signed:转unsigned(bit不变),mod2^k,转回来

+-*/
+-
Overflow and Underflow

在+时只能出现一次
+算出来,位数超了,砍掉超出位,按照Unsigned或者Two’s-complement 翻译,然后就出现了意想不到的效果。。。
overflow
unsigned:大于2^w:-2^w
signed:小于-2^w:+2^w
大于2^w:-2^w

怎么发现overflow

Overflow iff either:
u, v < 0, s ≥ 0 (NegOver)
u, v ≥ 0, s < 0 (PosOver)
ovf = (u<0 == v<0) && (u<0 != s<0);
ovf

additive inverse

Let UCompw (u ) = 2^w – u
UAddw(u , UCompw (u )) = 0

*/

<<算*2^k

3.Floating point encoding

Base-two version of scientific notation for representing real numbers

这里写图片描述
101.112 = 1*22 + 0*21 + 1*20 + 1*2-1 + 1*2-2
=5又3/4
类比十进制,不断*10,取走个位数
0.6875 10进制转二进制:不断*2,取走个位数
res x
1 1.375
0 0.750
1 1.500
1 1.000

0.1011 2进制= 0.6875 10进制

IEEE 754

Designed by W. Kahan for Intel processors (Turing Award 1989)
Based on a small and consistent set of principles, elegant, understandable, hard to make go fast

V = (-1)^s M 2^E
Sign bit s determines whether number is negative or positive
Significand M normally a fractional value in range [1.0,2.0).
Exponent E weights value by power of two
这里写图片描述
s is sign bit S
exp field encodes E
frac field encodes M
Sizes
Single precision (32 bits): 1 s, 8 exp, 23 frac
Double precision (64 bits): 1 s, 11 exp, 52 frac
Condition
exp ≠ 000…0 and exp ≠ 111…1

Exponent coded as biased value
E = exp – Bias
exp : unsigned value denoted by exp
Bias : Bias value
Single prec: 127 (exp: 1… 254, E: -126…127)
Double prec: 1023 (exp: 1…2046, E: -1022…1023)
In general: Bias = 2^(m-1) - 1, where m is the number of exponent bits
Significand coded with implied leading 1
M = 1.xxx…x2
xxx…x: bits of frac
Minimum when 000…0 (M = 1.0)
Maximum when 111…1 (M = 2.0 – )
Get extra leading bit for “free”

运算

&|^(异或Xor)~

可以用来swap
a^a=0

Logical Operators(0=false,1=true)&&, ||, !

shortcut:后面就不用算了

Left Shift: x << y

•左边多出来的丢掉
•右边补0

Right Shift: x >> y

•右边多出来的丢掉
–Logical shift
•左边补0
–Arithmetic shift
•最高位是什么就补什么
•Useful with two’s complement (特别是-的)

Mask Operations

X = 0x89ABCDEF
X & 0xFF =?

Byte Ordering

Little Endian

Least significant byte has lowest address
Alpha, PC

Big Endian

Least significant byte has highest address
Sun, Mac

Word Size

Indicating the nominal size of an address
现在是4byte=32bits或者8byte=64bits

参考文献:
复旦大学ICS课PPT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值