Overloading bit & Val

General FreeBASIC programming questions.
stephanbrunker
Posts: 62
Joined: Nov 02, 2013 14:57

Overloading bit & Val

Postby stephanbrunker » Jan 20, 2014 11:21

Hello,

so far, I succeeded in refining and further developing Richard's Bigint solution. New the TYPE bigint is nearly fully integrated:

The Header:

Code: Select all

Type Bigint     ' a little endian, two's complement binary number
   s As String ' packed into a string, in blocks four bytes long

   ' Constructors
   '-------------
   Declare Constructor ()  ' default constructor
   Declare Constructor (ByRef a As Byte)
   Declare Constructor (ByRef a As UByte)
   Declare Constructor (ByRef a As Short)
   Declare Constructor (ByRef a As UShort)
   Declare Constructor (ByRef a As Long)
   Declare Constructor (ByRef a As ULong)
   Declare Constructor (ByRef a As Integer)
   Declare Constructor (ByRef a As UInteger)
   Declare Constructor (ByRef a As LongInt)
   Declare Constructor (ByRef a As ULongInt)
   Declare Constructor (ByRef a As Double)
   Declare Constructor (ByRef a As String)

   ' let
   '----
   Declare Operator Let (ByRef a As Bigint)

   ' cast to other datatypes
   '------------------------
   Declare Operator Cast() As Byte     'CByte
   Declare Operator Cast() As UByte    'CUByte
   Declare Operator Cast() As Short    'CShort
   Declare Operator Cast() As UShort   'CUShort
   Declare Operator Cast() As Long     'CLng
   Declare Operator Cast() As ULong    'CULng
   Declare Operator Cast() As LongInt  'CLngint
   Declare Operator Cast() As ULongInt 'CULngint
   Declare Operator Cast() As Integer  'Cint
   Declare Operator Cast() As UInteger 'CUInt
   Declare Operator Cast() As Double   'CDbl
   Declare Operator Cast() As String   'Str

   
   ' functions
   '----------
   Declare Static Function compare (ByRef a As Bigint, ByRef b As Bigint) As Integer
   Declare Static Function square(ByRef aa As Bigint) As Bigint
   Declare Static Function mul2(ByRef a As Bigint) As Bigint
   Declare Static Function div2(ByRef a As Bigint) As Bigint
   Declare Static Sub div(ByRef aa As Bigint, ByRef bb As Bigint,ByRef q As Bigint, ByRef r As Bigint)
   Declare Static Function factorial (ByRef a As Bigint) As Bigint
   Declare Static Function modpower(ByRef bb As Bigint, ByRef ee As Bigint, ByRef m As Bigint) As Bigint
   Declare Static Sub prune(ByRef a As Bigint)
   Declare Static Function msbit(ByRef a As Bigint) As Integer
   Declare Static Function Bit_Value(ByRef v As Bigint, ByVal b As ULongInt) As Integer
   Declare Static Function Bit_Set(ByRef vv As Bigint, ByVal b As ULongInt) As Bigint
   Declare Static Function Bit_Reset(ByRef vv As Bigint, ByVal b As ULongInt) As Bigint

   ' implicit step versions
   Declare Operator For ()
   Declare Operator Step()
   Declare Operator Next(ByRef end_cond As Bigint) As Integer
   
   ' explicit step versions
   Declare Operator For (ByRef step_var As Bigint)
   Declare Operator Step(ByRef step_var As Bigint)
   Declare Operator Next(ByRef end_cond As Bigint, ByRef step_var As Bigint ) As Integer
   
   ' operate and assign
   Declare Operator += (ByRef rhs As Bigint)
   Declare Operator -= (ByRef rhs As Bigint)
   Declare Operator *= (ByRef rhs As Bigint)
   Declare Operator \= (ByRef rhs As Bigint)
   Declare Operator Mod= (ByRef rhs As Bigint)
   Declare Operator ^= (ByRef rhs As Bigint)
   Declare Operator Shl= (ByRef rhs As LongInt)
   Declare Operator Shr= (ByRef rhs As LongInt)
   Declare Operator And= (ByRef rhs As Bigint)
   Declare Operator Or= (ByRef rhs As Bigint)
   Declare Operator Xor= (ByRef rhs As Bigint)
   Declare Operator Imp= (ByRef rhs As Bigint)
   Declare Operator Eqv= (ByRef rhs As Bigint)

End Type

'================================================================
'    OVERLOADED OPERATORS:
'================================================================

     'Comparators
     '-----------
      Declare Operator <     (ByRef As Bigint, ByRef As Bigint) As Integer
      Declare Operator >     (ByRef As Bigint, ByRef As Bigint) As Integer
      Declare Operator =     (ByRef As Bigint, ByRef As Bigint) As Integer
      Declare Operator <>    (ByRef a As Bigint, ByRef b As Bigint) As Integer
      Declare Operator <=    (ByRef a As Bigint, ByRef b As Bigint) As Integer
      Declare Operator >=    (ByRef a As Bigint, ByRef b As Bigint) As Integer

     'Mathematical Operators
     '----------------------
      Declare Operator +     (ByRef x As Bigint) As Bigint
      Declare Operator -     (ByRef x As Bigint) As Bigint
      Declare Operator +     (ByRef x As Bigint, ByRef y As Bigint) As Bigint
      Declare Operator -     (ByRef x As Bigint, ByRef y As Bigint) As Bigint
      Declare Operator *     (ByRef x As Bigint, ByRef y As Bigint) As Bigint
      Declare Operator \     (ByRef x As Bigint, ByRef y As Bigint) As Bigint
      Declare Operator Mod   (ByRef x As Bigint, ByRef y As Bigint) As Bigint
      Declare Operator ^     (ByRef x As Bigint, ByRef y As LongInt) As Bigint
      Declare Operator abs   (Byref x As Bigint) As Bigint
      Declare Operator sgn   (Byref x As Bigint) As Integer      

     'Bitwise Operations
     '------------------
      Declare Operator Not   (ByRef x As Bigint) As Bigint
      Declare Operator And   (ByRef x As Bigint, ByRef y As Bigint) As Bigint
      Declare Operator Or    (ByRef x As Bigint, ByRef y As Bigint) As Bigint
      Declare Operator Xor   (ByRef x As Bigint, ByRef y As Bigint) As Bigint
      Declare Operator Imp   (ByRef x As Bigint, ByRef y As Bigint) As Bigint
      Declare Operator Eqv   (ByRef x As Bigint, ByRef y As Bigint) As Bigint
      Declare Operator shl   (Byref a As Bigint, ByVal n As LongInt) As Bigint
      Declare Operator shr   (Byref a As Bigint, ByVal n As LongInt) As Bigint

     'Conversion Functions
     '------------------
      Declare Function Bin(ByRef s As Bigint) As String
      Declare Function Hex(ByRef s As Bigint) As String
      Declare Function Uhex(ByRef s As Bigint) As String
      Declare Function Oct(ByRef s As Bigint) As String
      Declare Function MkBigint(ByRef a As Bigint) As String
      Declare Function MkUBigint(ByRef a As Bigint) As String
      Declare Function ValBigint(ByRef a As String) As Bigint
      Declare Function ValUBigint(ByRef a As String) As Bigint
      Declare Function CVBigint(ByRef aa As String) As Bigint
      Declare Function CVUBigint(ByRef aa As String) As Bigint


There're only a few things i couldn't menage:
a Function Cast(bigint, [ANY]) . The constructor works similarly, if you simply work with a (as bigint) = b (as any), but as far as I could evalutate, for a Cast(bigint,Integer), the TYPE Integer declaration needed to Content a cast() as bigint, which is not possible.

This is because the bit / bitset and bitreset macros actually contain such a cast() function and thus weren't overloadable or changeable.

And, if I want to Declare Function Val(ByRef a As String) As Bigint, the error occurs "Duplicated Definition".

(In another relation, it seems not possible to overload a Function XOR (a as string, b as string) as string which I need in crypto.)

And, if you wonder: I Need two Versions of HEX, because the bigint has no fixed length and is two's complement. So, the signed Version has a leading Zero block if the most significant bit is set and the value is positive, and in the unsigned Version, this leading block is not neccessary.

Return to “General”

Who is online

Users browsing this forum: Google [Bot] and 4 guests