data ConInt64 {
int8 value
}
const byte NEG_CHK_FLAG = 128
component provides SignedInt8 requires data.IntUtil iu {
ConInt64 con = new ConInt64()
byte conBytes[]
//perform a two's complement inversion to get the positive number
// - invert all bits using binary NOT, then add 1 to the result
int8 complement2(int8 val)
{
ConInt64 conX = new ConInt64(val)
byte conBytesX[] = dana.serial(conX)
for (int i = 0; i < conBytesX.arrayLength; i++)
{
conBytesX[i] = ~conBytesX[i]
}
conX.value += 1
return conX.value
}
//check for the sign bit
bool isNegativeV(opt int4 v)
{
bool negative = false
if (isset v)
{
ConInt64 conX = new ConInt64(v)
byte conBytesX[] = dana.serial(conX)
if ((conBytesX[0] & NEG_CHK_FLAG) == NEG_CHK_FLAG)
{
negative = true
}
}
else
{
if ((conBytes[0] & NEG_CHK_FLAG) == NEG_CHK_FLAG)
{
negative = true
}
}
return negative
}
SignedInt8:SignedInt8(opt int8 val, opt int8 neg)
{
if (isset val && isset neg)
{
throw new Exception("absolute and negative values cannot both be set (choose one)")
}
conBytes = dana.serial(con)
if (isset val)
{
con.value = val
}
else if (isset neg)
{
subtract(neg)
}
}
int8 SignedInt8:subtract(int8 val)
{
con.value = con.value - val
return con.value
}
int8 SignedInt8:add(int8 val)
{
con.value = con.value + val
return con.value
}
int8 SignedInt8:multiply(int8 val)
{
con.value = con.value * val
return con.value
}
int8 SignedInt8:divide(int8 val)
{
bool negA = isNegativeV()
bool negB = isNegativeV(val)
if (negA != negB)
{
//this implies one number is negative; the result will be negative
if (negA) con.value = complement2(con.value)
if (negB) val = complement2(val)
con.value = con.value / val
if (!isNegativeV()) con.value = complement2(con.value)
}
else if (negA)
{
//this implies both numbers are negative; the result will be positive
if (negA) con.value = complement2(con.value)
if (negB) val = complement2(val)
con.value = con.value / val
}
else
{
//both numbers are positive
con.value = con.value / val
}
return con.value
}
int8 SignedInt8:getValue()
{
return con.value
}
void SignedInt8:setValue(int8 v)
{
con.value = v
}
bool SignedInt8:isNegative()
{
return isNegativeV()
}
void SignedInt8:invert()
{
con.value = complement2(con.value)
}
byte[] SignedInt8:getBytes()
{
return clone conBytes
}
char[] SignedInt8:toString()
{
if (isNegative())
{
int8 val = complement2(con.value)
return "-$(val)"
}
else
{
return "$(con.value)"
}
}
}
To propose a new revision to this entity, use dana source put -uc your/new/version.dn -n data.SignedInt8 -m "reason for update" -u yourUsername
Version 1 (this version) by barry
Notes for this version: New compiler components