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

在C语言中,并没有直接表示一bit的数据类型。然而,我们可以通过使用位域(bit fields)操作来实现对单个bit的控制。以下是一些常见的方法: 1. **使用位域(bit fields)**: 位域允许我们定义一个结构体,其中每个成员可以指定占用多少个bit。 ```c #include <stdio.h> struct BitField { unsigned int bit0 : 1; unsigned int bit1 : 1; unsigned int bit2 : 1; unsigned int bit3 : 1; unsigned int bit4 : 1; unsigned int bit5 : 1; unsigned int bit6 : 1; unsigned int bit7 : 1; }; int main() { struct BitField bf; bf.bit0 = 1; bf.bit1 = 0; bf.bit2 = 1; bf.bit3 = 0; bf.bit4 = 1; bf.bit5 = 0; bf.bit6 = 1; bf.bit7 = 0; printf("Bit0: %u\n", bf.bit0); printf("Bit1: %u\n", bf.bit1); printf("Bit2: %u\n", bf.bit2); printf("Bit3: %u\n", bf.bit3); printf("Bit4: %u\n", bf.bit4); printf("Bit5: %u\n", bf.bit5); printf("Bit6: %u\n", bf.bit6); printf("Bit7: %u\n", bf.bit7); return 0; } ``` 2. **使用位操作**: 通过位操作,我们可以将一个字节的特定位设置为0或1。 ```c #include <stdio.h> int main() { unsigned char byte = 0; // 初始值为0 // 设置第0位为1 byte |= (1 << 0); // 设置第2位为1 byte |= (1 << 2); // 设置第4位为1 byte |= (1 << 4); // 设置第6位为1 byte |= (1 << 6); printf("Byte: %u\n", byte); // 检查第0位 if (byte & (1 << 0)) { printf("Bit0 is set\n"); } else { printf("Bit0 is not set\n"); } // 检查第1位 if (byte & (1 << 1)) { printf("Bit1 is set\n"); } else { printf("Bit1 is not set\n"); } return 0; } ``` 通过以上方法,我们可以在C语言中表示控制单个bit的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值