Click here to Skip to main content
15,899,679 members
Home / Discussions / C#
   

C#

 
GeneralRe: Connection string problem Pin
Ravenet11-Feb-08 3:10
Ravenet11-Feb-08 3:10 
QuestionHow to Add a Font in Setup Project Pin
Bajrang Singh10-Feb-08 20:04
Bajrang Singh10-Feb-08 20:04 
AnswerRe: How to Add a Font in Setup Project Pin
Rupesh Kumar Swami10-Feb-08 20:18
Rupesh Kumar Swami10-Feb-08 20:18 
AnswerRe: How to Add a Font in Setup Project Pin
Giorgi Dalakishvili10-Feb-08 20:25
mentorGiorgi Dalakishvili10-Feb-08 20:25 
GeneralRe: How to Add a Font in Setup Project Pin
Rupesh Kumar Swami10-Feb-08 20:39
Rupesh Kumar Swami10-Feb-08 20:39 
GeneralAn unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll in c# Pin
ramyanaidu10-Feb-08 18:36
ramyanaidu10-Feb-08 18:36 
GeneralRe: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll in c# Pin
Martin#10-Feb-08 20:24
Martin#10-Feb-08 20:24 
GeneralPrinting 128-bit integer in decimal Pin
Skippums10-Feb-08 15:59
Skippums10-Feb-08 15:59 
I have created an Int128 class, but am having trouble with my ToString method (converting to base 10). I cannot figure out why I am getting incorrect results. The applicable code is as follows (within a class named "TestUInt"):
private ulong m_LBits, m_HBits;

public TestUInt(ulong high, ulong low) {
    m_HBits = high;
    m_LBits = low;
}

private static void Divide(TestUInt dividend, TestUInt divisor, out TestUInt quotient, out TestUInt remainder) {
    if (divisor == 0ul)
        throw new DivideByZeroException();
    quotient = 0;
    remainder = 0;
    if (divisor < dividend) {
        remainder = dividend;
    } else if (dividend == divisor) {
        quotient = 1;
    } else if (dividend != 0ul) {
        int divBits = Log2(dividend);
        int performedOps = 0;
        int requiredOps = divBits - Log2(divisor);
        for (TestUInt comparator = divisor << requiredOps; divBits >= 64
            && performedOps <= requiredOps; comparator >>= 1, ++performedOps, --divBits) {
            quotient <<= 1;
            if (dividend >= comparator) {
                dividend -= comparator;
                quotient.m_LBits |= 1;
            }
        }
        if (performedOps <= requiredOps) {
            quotient <<= requiredOps - performedOps + 1;
            quotient.m_LBits |= dividend.m_LBits / divisor.m_LBits;
            dividend = dividend.m_LBits % divisor.m_LBits;
        }
        remainder = dividend;
    }
}
    
private static int Log2(TestUInt value) {
    if (value.m_HBits != 0)
        return MathExtension.Log2(value.m_HBits) + 64;
    else
        return MathExtension.Log2(value.m_LBits);
}

public static TestUInt operator -(TestUInt a, TestUInt b) {
    ulong old_lbits = a.m_LBits;
    a.m_LBits -= b.m_LBits;
    a.m_HBits -= b.m_HBits;
    if (a.m_LBits > old_lbits)
        --a.m_HBits;
    return a;
}

public static TestUInt operator <<(TestUInt a, int b) {
    b &= 127;
    if (b == 0) {
        return new TestUInt(a.m_HBits, a.m_LBits);
    } else if (b >= 64) {
        return new TestUInt(a.m_LBits << b - 64, 0);
    } else {
        return new TestUInt(a.m_HBits << b | a.m_LBits >> 64 - b, a.m_LBits << b);
    }
}

public static TestUInt operator >>(TestUInt a, int b) {
    b &= 127;
    if (b == 0) {
        return new TestUInt(a.m_HBits, a.m_LBits);
    } else if (b >= 64) {
        return new TestUInt(0, a.m_HBits >> b - 64);
    } else {
        return new TestUInt(a.m_HBits >> b, a.m_HBits << 64 - b | a.m_LBits >> b);
    }
}

public static bool operator ==(TestUInt a, TestUInt b) {
    return (a.m_LBits == b.m_LBits && a.m_HBits == b.m_HBits);
}

public static bool operator !=(TestUInt a, TestUInt b) {
    return (a.m_LBits != b.m_LBits || a.m_HBits != b.m_HBits);
}

public static bool operator <=(TestUInt a, TestUInt b) {
    return (a.m_HBits == b.m_HBits ? a.m_LBits <= b.m_LBits : a.m_HBits <= b.m_HBits);
}

public static bool operator >=(TestUInt a, TestUInt b) {
    return (a.m_HBits == b.m_HBits ? a.m_LBits >= b.m_LBits : a.m_HBits >= b.m_HBits);
}

public static bool operator <(TestUInt a, TestUInt b) {
    return (a.m_HBits == b.m_HBits ? a.m_LBits < b.m_LBits : a.m_HBits < b.m_HBits);
}

public static bool operator >(TestUInt a, TestUInt b) {
    return (a.m_HBits == b.m_HBits ? a.m_LBits > b.m_LBits : a.m_HBits > b.m_HBits);
}

public static implicit operator TestUInt(ulong value) {
    return new TestUInt(0, value);
}
    
public override string ToString() {
    TestUInt quotient = this, remainder;
    string rval = string.Empty;
    while (quotient.m_HBits != 0) {
        Divide(quotient, 1000000000000000000ul, out quotient, out remainder);
        rval = remainder.m_LBits.ToString("D18") + rval;
    }
    rval = quotient.m_LBits.ToString("D") + rval;
    return rval;
}
I know that the 'Divide' method is not working (or my theory on how to print integers in base 10 is wrong), because if I change the method "ToString" to use, say 1000 instead of 1000000000000000000, I get a different output. My test application works as follows:
string line = "{0,-3} {1,-3}\r\n       {2}\r\n       {3}";
// 101739724956510982573881893874860769099
UInt128 c = new UInt128(0x4C8A5BB86B074FBD, 0x7A223174473F3F4B);
// 21804722866621884125668607625
UInt128 d = new UInt128(0x00000000467471AB, 0x1F2DA3680D768689);
// 18446744073709551618 
UInt128 e = new UInt128(1, 2); // 0x10000000000000002
// 68719476736
UInt128 f = 0x1000000000;      // 0x00000001000000000
// 330888471822165082112
UInt128 g = new UInt128(0x11, 0xF000000000000000); // 0x11F000000000000000
// 20476206912383221760
UInt128 h = new UInt128(0x01, 0x1C2A19B300000000); // 0x011C2A19B300000000

Console.WriteLine(string.Format(line, "Var", "Frm", "Result", "Computed"));
// Produces correct output, until I change the divisor to 1000 in ToString
Console.WriteLine(string.Format(line, "c", "", "101739724956510982573881893874860769099", c.ToString()));
// Produces two different but wrong results using either constant within ToString
Console.WriteLine(string.Format(line, "d", "", "21804722866621884125668607625", d.ToString()));
// Produces the same incorrect result using either constant within ToString
Console.WriteLine(string.Format(line, "e", "", "18446744073709551618", e.ToString()));
// Produces the same incorrect result using either constant within ToString
Console.WriteLine(string.Format(line, "h", "", "20476206912383221760", h.ToString()));
Again, sorry for the long post, but hopefully someone is willing to give me some pointers here as to why my ToString method isn't working. Thanks,

Sounds like somebody's got a case of the Mondays

-Jeff

GeneralRe: Printing 128-bit integer in decimal Pin
Luc Pattyn11-Feb-08 1:41
sitebuilderLuc Pattyn11-Feb-08 1:41 
GeneralRe: Printing 128-bit integer in decimal Pin
Skippums11-Feb-08 5:20
Skippums11-Feb-08 5:20 
GeneralRe: Printing 128-bit integer in decimal Pin
Luc Pattyn11-Feb-08 6:06
sitebuilderLuc Pattyn11-Feb-08 6:06 
AnswerRe: Printing 128-bit integer in decimal Pin
Skippums11-Feb-08 5:50
Skippums11-Feb-08 5:50 
QuestionBackground image refreshing on Form resizing? Pin
Jacob Dixon10-Feb-08 13:19
Jacob Dixon10-Feb-08 13:19 
AnswerRe: Background image refreshing on Form resizing? Pin
Jacob Dixon10-Feb-08 13:27
Jacob Dixon10-Feb-08 13:27 
QuestionNew gradient background for Button...? Pin
Edmundisme10-Feb-08 11:23
Edmundisme10-Feb-08 11:23 
AnswerRe: New gradient background for Button...? Pin
Joachim Kerschbaumer10-Feb-08 21:48
Joachim Kerschbaumer10-Feb-08 21:48 
GeneralRe: New gradient background for Button...? Pin
Edmundisme11-Feb-08 7:37
Edmundisme11-Feb-08 7:37 
AnswerRe: New gradient background for Button...? Pin
Abhijit Jana10-Feb-08 22:03
professionalAbhijit Jana10-Feb-08 22:03 
GeneralRe: New gradient background for Button...? Pin
Edmundisme11-Feb-08 7:38
Edmundisme11-Feb-08 7:38 
AnswerRe: New gradient background for Button...? Pin
Ravenet11-Feb-08 3:13
Ravenet11-Feb-08 3:13 
GeneralRe: New gradient background for Button...? Pin
Edmundisme11-Feb-08 7:38
Edmundisme11-Feb-08 7:38 
GeneralAssigning a control back to the main form in the DragOver method... [modified] Pin
new_phoenix10-Feb-08 9:13
new_phoenix10-Feb-08 9:13 
GeneralApplication of Digital Dictionary Pin
Abdul8510-Feb-08 7:23
Abdul8510-Feb-08 7:23 
GeneralDouble post - please ignore Pin
pmarfleet10-Feb-08 7:51
pmarfleet10-Feb-08 7:51 
GeneralBest approach Pin
pokabot10-Feb-08 6:41
pokabot10-Feb-08 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.