3.2

3.2 | 5 ratings Rate this file 19 Downloads (last 30 days) File Size: 417 Bytes File ID: #13426

from decimal to two's complement

by Moatasem Chehaiber

 

17 Dec 2006 (Updated 18 Dec 2006)

a function to convert a decimal number into 2's complement number of defined size

| Watch this File

File Information
Description

The function coverts a decimal number into a two's compelement number of a given in argument number of bits.

Acknowledgements
This submission has inspired the following:
Two's Complement
MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
31 Jan 2007 Nastase Dan  
22 Mar 2007 fatima abd  
18 Jun 2007 47 44

so who loves hockey around here? eh?

18 Jun 2007 DontSubmit CrapPrograms

this is a very bad program,
Flaw 1) It doesnt work, e.g. heres what i get
dec2bit(0.3,5) = 00100, ok, then
dec2bit(-0.3,5) gives me just the 1's complement = 11011, but it should be 11100.

13 Nov 2010 Hassan Naseri

I wrote two simple functions, to convert between decimal and two's complement integers:

function value = dec2tc(dec, N)
value = dec2bin(mod((dec),2^N),N);
end

function value = tc2dec(bin,N)
val = bin2dec(bin);
y = sign(2^(N-1)-val)*(2^(N-1)-abs(2^(N-1)-val));
if ((y == 0) && (val ~= 0))
  value = -val;
else
    value = y;
end
end

29 Jun 2011 Gorka  
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
wireless Moatasem Chehaiber 22 Oct 2008 08:53:03
twos complement Moatasem Chehaiber 22 Oct 2008 08:53:03
decimal Moatasem Chehaiber 22 Oct 2008 08:53:03
convert Moatasem Chehaiber 22 Oct 2008 08:53:03
wireless communications Moatasem Chehaiber 22 Oct 2008 08:53:03

Contact us at files@mathworks.com