Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- classdef bin
- % Class for binary numbers of up to 2^16 digits
- % Main property is Expo, the vector of exponents, e.g. [0 1 2] for 7.
- % bin([]) == bin(0,'dec') == bin('0') == bin('') == bin
- % bin([0 1 2]) == bin(7,'dec') == bin('111')
- % bin([m n ...],'pre') preallocates an array of size [m n ...] (use [m 1] or [1 m] for vectors)
- properties
- Expo % exponents where the binary number has a 1
- Weight % length of Expo, number of binary 1s
- Maxi % highest exponent (Maxi+1 is the length of a positive binary number)
- end
- methods
- function o = bin(x,parameter) % constructor
- if nargin == 0
- o = bin('') ;
- elseif nargin==1
- if isnumeric(x)
- if ~isempty(x)
- o.Expo = uint16( sort(x) ) ;
- o.Weight = uint16( length(o.Expo) ) ;
- else
- o.Expo = uint16( zeros(1,0) ) ;
- o.Weight = uint16( 0 ) ;
- end
- else
- long = length(x) ;
- o.Weight = uint16( sum( char2mat(x) ) ) ;
- expo = uint16( zeros(1,o.Weight) ) ;
- count = 1 ;
- for m=0:long-1
- if x(long-m)=='1'
- expo(count) = m ;
- count = count+1 ;
- end
- end
- o.Expo = expo ;
- end
- o.Maxi = max(o.Expo) ;
- else
- if strcmp(parameter,'dec') % dec for decimal, intended for integers up to 2^16
- if x==0
- o = bin('0') ;
- else
- long = floor( log2(double(x)) ) + 1 ;
- X = double(x) ;
- str = repmat('0',1,long) ;
- count = long-1;
- while count >= 0
- rest = X - 2^count ;
- if rest >= 0
- X = rest ;
- str(long-count) = '1' ;
- end
- count = count - 1 ;
- end
- o = bin(str) ;
- end
- elseif strcmp(parameter,'pre') % pre for pre-allocate
- o = repmat( bin , x ) ;
- end
- end
- end
- function y = sym(o) % to symbolic object (overload)
- s = sym(0) ;
- for m=1:o.Weight
- s = s + 2^(sym( o.Expo(m) )) ;
- end
- y = s ;
- end
- function y = uint16(o) % to uint16 (overload)
- s = uint16(0) ;
- for m=1:o.Weight
- s = s + 2^o.Expo(m) ;
- end
- y = s ;
- end
- function y = string(o) % to string
- if ~isequal( bin([]) , o )
- long = o.Maxi + 1 ;
- s = repmat('0',1,long) ;
- for m=1:o.Weight
- s( long - o.Expo(m) ) = '1' ;
- end
- y = s ;
- else
- y = '0' ;
- end
- end
- function y = bitand(x1,x2) % overload bitand
- y = bin( intersect(x1.Expo,x2.Expo) ) ;
- end
- function y = bitor(x1,x2) % overload bitor
- y = bin( union(x1.Expo,x2.Expo) ) ;
- end
- function y = bitxor(x1,x2) % overload bitxor
- y = bin( setdiff( union(x1.Expo,x2.Expo) , intersect(x1.Expo,x2.Expo) ) ) ;
- end
- function y = eq(x1,x2) % overload ==
- y = isequal(x1,x2) ;
- end
- function y = ne(x1,x2) % overload ~=
- y = ~isequal(x1,x2) ;
- end
- function y = plus(x1,x2) % overload +
- e1 = x1.Expo ;
- e2 = x2.Expo ;
- OR = union(e1,e2) ;
- AND = intersect(e1,e2) ;
- XOR = setdiff(OR,AND) ;
- if ~isempty(AND) % if there is overflow
- y = bin(XOR) + bin(AND+1) ; % recursion
- else
- y = bin(XOR) ;
- end
- end
- function y = mtimes(x1,x2) % overload *
- if x1.Maxi > x2.Maxi
- a = x2 ;
- b = x1 ;
- else
- a = x1 ;
- b = x2 ;
- end % if one of the binary numbers is longer, that is now b
- s = bin('0') ; % sum
- for m = 1 : a.Weight
- s = s + bin( b.Expo + a.Expo(m) ) ;
- end
- y = s ;
- end
- end
- end
- %{
- function [y] = char2mat(x)
- % Turns a matrix of type CHAR without spaces
- % into a matrix of type DOUBLE.
- high = size(x,1) ;
- wide = size(x,2) ;
- Y = zeros(high,wide) ;
- for m = 1:high
- for n = 1:wide
- Y(m,n) = str2double( x(m,n) ) ;
- end
- end
- y = Y ;
- end
- %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement