Advertisement
mate2code

Matlab class for large binary numbers

Feb 25th, 2013
1,019
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.17 KB | None | 0 0
  1. classdef bin
  2.     % Class for binary numbers of up to 2^16 digits
  3.     % Main property is Expo, the vector of exponents, e.g. [0 1 2] for 7.
  4.     % bin([])      == bin(0,'dec') == bin('0')   == bin('') == bin
  5.     % bin([0 1 2]) == bin(7,'dec') == bin('111')
  6.     % bin([m n ...],'pre') preallocates an array of size [m n ...] (use [m 1] or [1 m] for vectors)
  7.  
  8.     properties
  9.         Expo  % exponents where the binary number has a 1
  10.         Weight  % length of Expo, number of binary 1s
  11.         Maxi  % highest exponent (Maxi+1 is the length of a positive binary number)
  12.     end
  13.    
  14.     methods    
  15.        
  16.         function o = bin(x,parameter)  % constructor
  17.             if nargin == 0
  18.                 o = bin('') ;
  19.             elseif nargin==1
  20.                 if isnumeric(x)
  21.                     if ~isempty(x)
  22.                         o.Expo   = uint16(   sort(x)          ) ;
  23.                         o.Weight = uint16(   length(o.Expo)   ) ;
  24.                     else
  25.                         o.Expo   = uint16(   zeros(1,0)       ) ;
  26.                         o.Weight = uint16(   0                ) ;
  27.                     end
  28.                 else
  29.                     long = length(x) ;
  30.                     o.Weight = uint16(   sum( char2mat(x) )   ) ;
  31.                     expo     = uint16(   zeros(1,o.Weight)    ) ;
  32.                     count = 1 ;
  33.                     for m=0:long-1
  34.                         if x(long-m)=='1'
  35.                             expo(count) = m ;
  36.                             count = count+1 ;
  37.                         end
  38.                     end
  39.                     o.Expo = expo ;
  40.                 end
  41.                 o.Maxi = max(o.Expo) ;
  42.             else
  43.                 if strcmp(parameter,'dec')  % dec for decimal, intended for integers up to 2^16
  44.                     if x==0
  45.                         o = bin('0') ;
  46.                     else
  47.                         long = floor( log2(double(x)) ) + 1 ;
  48.                         X = double(x) ;
  49.                         str = repmat('0',1,long) ;
  50.                         count = long-1;
  51.                         while count >= 0
  52.                             rest = X - 2^count ;
  53.                             if rest >= 0
  54.                                 X = rest ;
  55.                                 str(long-count) = '1' ;
  56.                             end
  57.                             count = count - 1 ;
  58.                         end
  59.                         o = bin(str) ;
  60.                     end
  61.                 elseif strcmp(parameter,'pre')  % pre for pre-allocate
  62.                     o = repmat( bin , x ) ;
  63.                 end
  64.             end
  65.         end
  66.        
  67.         function y = sym(o)  % to symbolic object (overload)
  68.             s = sym(0) ;
  69.             for m=1:o.Weight
  70.                 s = s + 2^(sym( o.Expo(m) )) ;
  71.             end
  72.             y = s ;
  73.         end
  74.         function y = uint16(o)  % to uint16 (overload)
  75.             s = uint16(0) ;
  76.             for m=1:o.Weight
  77.                 s = s + 2^o.Expo(m) ;
  78.             end
  79.             y = s ;
  80.         end
  81.         function y = string(o)  % to string
  82.             if ~isequal( bin([]) , o )
  83.                 long = o.Maxi + 1 ;
  84.                 s = repmat('0',1,long) ;
  85.                 for m=1:o.Weight
  86.                     s( long - o.Expo(m) ) = '1' ;
  87.                 end
  88.                 y = s ;
  89.             else
  90.                 y = '0' ;
  91.             end
  92.         end  
  93.         function y = bitand(x1,x2)  % overload bitand
  94.             y = bin( intersect(x1.Expo,x2.Expo) ) ;
  95.         end
  96.         function y = bitor(x1,x2)  % overload bitor
  97.             y = bin( union(x1.Expo,x2.Expo) ) ;
  98.         end
  99.         function y = bitxor(x1,x2)  % overload bitxor
  100.             y = bin(   setdiff( union(x1.Expo,x2.Expo) , intersect(x1.Expo,x2.Expo) )   ) ;
  101.         end
  102.         function y = eq(x1,x2)  % overload ==
  103.             y = isequal(x1,x2) ;
  104.         end
  105.         function y = ne(x1,x2)  % overload ~=
  106.             y = ~isequal(x1,x2) ;
  107.         end      
  108.         function y = plus(x1,x2)  % overload +
  109.             e1 = x1.Expo ;
  110.             e2 = x2.Expo ;
  111.             OR = union(e1,e2) ;
  112.             AND = intersect(e1,e2) ;
  113.             XOR = setdiff(OR,AND) ;
  114.             if ~isempty(AND)  % if there is overflow
  115.                 y = bin(XOR) + bin(AND+1) ;  % recursion
  116.             else
  117.             y = bin(XOR) ;
  118.             end
  119.         end  
  120.         function y = mtimes(x1,x2)  % overload *
  121.             if x1.Maxi > x2.Maxi
  122.                 a = x2 ;
  123.                 b = x1 ;
  124.             else
  125.                 a = x1 ;
  126.                 b = x2 ;
  127.             end  % if one of the binary numbers is longer, that is now b
  128.             s = bin('0') ;  % sum
  129.             for m = 1 : a.Weight
  130.                 s = s + bin( b.Expo + a.Expo(m) ) ;
  131.             end
  132.             y = s ;
  133.         end
  134.  
  135.     end
  136.    
  137. end
  138.  
  139. %{
  140. function [y] = char2mat(x)
  141. % Turns a matrix of type CHAR without spaces
  142. % into a matrix of type DOUBLE.
  143.     high = size(x,1) ;
  144.     wide = size(x,2) ;
  145.     Y = zeros(high,wide) ;
  146.     for m = 1:high
  147.         for n = 1:wide
  148.             Y(m,n) = str2double( x(m,n) ) ;
  149.         end
  150.     end
  151.     y = Y ;
  152. end
  153. %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement