Skip to content

Commit

Permalink
update csapp 2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
SliverHorn committed Jun 5, 2024
1 parent a629361 commit 95b3aff
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions content/csapp/2.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
title: "Integer Arithmetic"
tags: ["csapp"]
categories: ["csapp"]
date: 2021-06-04T17:17:17+07:00
draft: false
---

Integer Arithmetic(整数运算)

<!--more-->

## Unsigned Addition

For x and y $0 \leq x \leq 2^w$ , $0 \leq y \leq 2^w$

$$
x+_{w}^{u}y= \begin{cases} x+y, & \text {x + y < $2^w$} \\\\ 3n+1, & \text{$2^{w}\leq{x+y}{\leq}2^{w+1}$} \end{cases}
$$

## Additive Inverse

For x,$0 \leq x \leq 2^w$

$$
x + x^{'} = x^{'} + x = 0
$$

$$
y - x => y + x^{'}
$$

For $x, x^{'} => 0 \leq x \leq 2^{w}, 0 \leq x^{'} < 2^{w}$

$$
x + x^{'} = 2^{w} = 0
$$

$$
-_{w}^{u}y= \begin{cases} x, & \text {x = 0} \\\\ 2^{w}-x, & \text{x $\geq$ 0} \end{cases}
$$

## Two's Complement Addition

For x and y $-2^{w-1}{\leq}{x}{\leq}2^{w-1}-1, -2^{w-1}{\leq}{y}{\leq}2^{w-1}-1$

$$
x+_{w}^{t}y= \begin{cases} x+y-2^{w}, & \text {$2^{w-1}{\leq}{x+y}$(Positive overflow)} \\\\ x+y, & \text{$-2^{w-1}\leq{x+y}{<}2^{w-1}$} \\\\ x+y+2^{w}, & \text{x+y<$-2^{w-1}$(Negative overflow)} \end{cases}
$$

## Two's Complement Addition Code

```c
#include <stdio.h>

int main() {
char x = 127;
char y = 1;
char z = x + y;
printf("z=%d",z);
return 0;
}
```

## Two's Complement Negation

For x $-2^{w-1}{\leq}x<2^{w-1}-1$

$$
-_{w}^{t}x= \begin{cases} -x, & \text {$x > TMin_w$} \\\\ TMin_{w}, & \text{$x=TMin_{w}$} \end{cases}
$$

$$
|Tmin_x| = |Tmax_w| +1
$$

$$
Tmin_w + Tmin_w = -2^{w-1}+(-2^{w-1})=-2^{w}
$$

$$
Tmin_w+_{w}^{t}Tmain_w = -2^w+2^w=0
$$


0 comments on commit 95b3aff

Please sign in to comment.