2’s Complement and Floating-Point
2¡¯s Complement and
Floating-Point
CSE 351 Section 3
Two¡¯s Complement
?An n-bit, two¡¯s complement
number can represent the
range [?2 ??1 , 2 ??1 ? 1].
?
Note the asymmetry of this
range about 0 ¨C there¡¯s one more
negative number than positive
?Note what happens when
you overflow
4-bit two¡¯s complement range
Understanding Two¡¯s Complement
?
An easier way to find the decimal value of a two¡¯s complement number:
~x + 1 = -x
?
We can rewrite this as x = ~(-x - 1), i.e. subtract 1 from the given number,
and flip the bits to get the positive portion of the number.
?
Example: 0b11010110
?
Subtract 1: 0b11010110 - 1 = 0b11010101
?
Flip the bits: ~0b11010101 = 0b00101010
?
Convert to decimal: 0b00101010 = (32+8+2)10 = 4210
?
Multiply by negative one, Answer: -4210.
Two¡¯s Complement: Operations
1. Convert numbers to binary
int
int
int
int
x = 0xAB;
y = 17;
z = 5;
result = ~(x | y) + z;
What is the value of
result in decimal?
?
0xAB = 0b10101011
?
1710 = 0b00010001
2. Compute x | y
0000 ... 1010 1011
| 0000 ... 0001 0001
-------------------0000 ... 1011 1011
3. Compute ~(x | y) (flip the bits)
~ 0000 0000 0000 0000 0000 0000 1011 1011
----------------------------------------1111 1111 1111 1111 1111 1111 0100 0100
Two¡¯s Complement: Operations
4. Compute ~(x | y) + z
int
int
int
int
x = 0xAB;
y = 17;
z = 5;
result = ~(x | y) + z;
What is the value of
result in decimal?
?
You can either convert to decimal before or after
adding z
?
1710 = 0b00010001
?
Convert
111111111111111111111111010001002 to
decimal
(hint: -x = ~x + 1)
?
~111111111111111111111111010001002 =
0000000000000000000000000101110112
?
~0000¡¡101110112 + 1 = 0000¡¡101111002
?
-x = 0000¡¡101111002
?
x = 1000¡¡101111002
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- introduction to python computer science
- 2d arrays 2d arrays and double pointers bryn mawr
- types and streams
- 2 s complement and floating point
- operators and expressions
- section 2 1 variables int
- interfacing c c and python with swig
- data types in python
- lecture 2 types and structs
- topic 6 nested nested for loops university of texas at
Related searches
- 1 s complement and 2 s complement converter
- 2 s complement negative number
- 2 s complement form
- adding 3 values 2 s complement numbers calculator
- n bit 2 s complement largest value
- 2 s complement subtraction calculator
- hexadecimal to 2 s complement calculator
- 8 bit 2 s complement calculator
- adding 2 s complement numbers
- what is 2 s complement form
- 2 s complement addition calculator
- 2 s complement addition