0% found this document useful (0 votes)
69 views9 pages

Subtracting and Converting Integers

This document discusses digital systems and numeric data types including unsigned and signed integers. It covers subtracting and adding unsigned integers, the two's complement representation of signed integers, resizing signed integers, and adding and subtracting signed integers including overflow detection. Conversion between different numeric types like std_logic, signed, unsigned and integer is also covered.

Uploaded by

ingjojeda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views9 pages

Subtracting and Converting Integers

This document discusses digital systems and numeric data types including unsigned and signed integers. It covers subtracting and adding unsigned integers, the two's complement representation of signed integers, resizing signed integers, and adding and subtracting signed integers including overflow detection. Conversion between different numeric types like std_logic, signed, unsigned and integer is also covered.

Uploaded by

ingjojeda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Digital Systems - 5

Pere Pal`a - Alexis L


opez
iTIC [Link]

February 2015

Substracting Unsigned Integers

signal u1 , u2 , r1 , r2
...
u1 <= " 0011 " ; -- 3
u2 <= " 0100 " ; -- 4
r1 <= u1 - u2 ;
r2 <= u2 - u1 ;
I "1111" --> -1
I "1110" --> -2
I

...

: unsigned (3 downto 0);

-- > "1111"
-- > "0001"

Signed Integers
Definition
I

x = xn1 2n1 + xn2 2n2 + + x0 20 .

Only difference: sign of xn1

It is called 2s complement

Use
I

Usable range: 2n1 2n1 1

Not symmetric around zero!

signal my_signed
: signed
(3 downto 0);
signal my_unsigned : unsigned (3 downto 0);
my_signed <= " 0000 " ;
my_unsigned <= my_signed ;
-- Illegal : d i f f e r e n t types !
my_unsigned <= unsigned ( my_signed ); -- Ok : Type c o n v e r s i o n
my_signed
<= signed ( my_unsigned ); -- Ok : Type c o n v e r s i o n

Twos Complement
code
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

n
0
1
2
3
4
5
6
7
-8
-7
-6
-5
-4
-3
-2
-1

Sign change
I

Convert 5 to -5
1. Invert 0101 1010
2. Add 1 1011

Convert -5 to 5
1. Invert 1011 0100
2. Add 1 0101

x_neg <= not x + 1;

Resizing Signed Integers


Expand
signal a
: signed (3 downto 0);
signal b1 , b2 , b3 : signed (7 downto 0);
b1 <= a (3) & a (3) & a (3) & a (3) & a ;
b2 (3 downto 0) <= a ;
b2 (7 downto 4) <=( others = > a (3));
b3 <= resize (a ,8);
b3 <= resize (a , b3 length );

Truncate
signal a1 , a2 : signed (3 downto 0);
signal b
: signed (7 downto 0);
a1 <= b (3 downto 0);
a2 <= resize (b ,4);
a2 <= resize (b , a2 length );

Adding Signed Integers

1 1 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

72:
49:

0 1 0 0 1 0 0 0
0 0 1 1 0 0 0 1

63:
32:

1 1 0 0 0 0 0 1
1 1 1 0 0 0 0 0

42:
8:

1 1 0 1 0 1 1 0
0 0 0 0 1 0 0 0

121:

0 1 1 1 1 0 0 1

95:

1 0 1 0 0 0 0 1

34:

1 1 0 1 1 1 1 0

no overflow

no overflow

0 1 0 0 1 0 0 0

72:
105:

0 1 0 0 1 0 0 0
0 1 1 0 1 0 0 1
1 0 1 1 0 0 0 1

positive overflow

no overflow

1 0 0 0 0 0 0 0

63:
96:

1 1 1 1 1 0 0 0

1 1 0 0 0 0 0 1
1 0 1 0 0 0 0 0

2:
8:

0 0 1 0 1 0 1 0
1 1 1 1 1 0 0 0

0 1 1 0 0 0 0 1

34:

0 0 1 0 0 0 1 0

negative overflow

The same hardware as for unsigned integers.

no overflow

Substracting Signed Integers

x y = x + (y )

I rslt <= x + (not y + 1);


n

add/sub

xn1 x1 x0

ovf/und
signed
ovf

cn
cn1

sn1

yn1

adder
s1

y1

c0
s0

sn1/dn1

s1/d1

y0

s0/d0

Abstract Numeric Types

Take care of the numeric aspects of data...

... and not the binary encoding.

I natural range 0 to 2**N-1


signal my_nat : natural range 0 to 255;
signal my_uns : unsigned (7 downto 0);
I integer range -2**(N-1) to 2**(N-1)-1
signal my_int : integer range -128 to 127;
signal my_sig : signed (7 downto 0);

Conversions

to
from

std_logic

std_logic

signed

unsigned

integer

signed()

unsigned()

std_logic

unsigned()

to_integer()

signed()

to_integer()

to_signed

to_unsigned

(,length)

(,length)

_vector

_vector
signed

_vector()
unsigned

std_logic
_vector()

integer

You might also like