Code covered by the BSD License  

Highlights from
Extended Brookshear Machine emulator and assembler

image thumbnail
from Extended Brookshear Machine emulator and assembler by David Young
Emulator and assembler for a simple computer, a teaching aid for computer science courses.

dble2int8(d)
function i = dble2int8(d)
%DBLE2INT8 converts double to 1-byte signed integer
%   I = DBLE2INT8(D) stores the value of D as a signed 8-bit integer. The
%   value is returned as the rightmost 8 bits of a uint32.

%   Copyright 2008 University of Sussex and David Young

if d ~= floor(d) || d > 127 || d < -128
    error('Integer value in range -128 to 127 required');
end
if d < 0
    d = d + 256;
end
i = uint32(d);

end

Contact us at files@mathworks.com