D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8382 - std.bitmanip opCmp bug or user error?
Summary: std.bitmanip opCmp bug or user error?
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-07-12 17:56 UTC by Tracey
Modified: 2020-03-21 03:56 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tracey 2012-07-12 17:56:42 UTC
Comparing two DIFFERENT BitArrays with ">", "<", or "==" whose lengths are greater than 8 bits always returns EQUALITY.

===========================================
The following code demonstrates my problem:
===========================================

import std.stdio, std.bitmanip;

/* Write out the BitArray, LSB->MSB */
void to_Bin(ref BitArray ba) {
    foreach(i; 0..ba.length)
        write(cast(ubyte)ba[i]);
    writeln;
}

/* Display the result of the BitArray comparison */
void checkCmp(BitArray a, BitArray b, string name_a, string name_b) {

    if(a > b) {
        writeln(name_a, " > ", name_b);
    }
    else if (b > a) {
        writeln(name_b, " > ", name_a);
    }
    else {
        writeln(name_a, " = ", name_b);
    }
}

void main() {

    // BitPos:   0  1  2  3  4  5  6  7 |0  1  2  3  4  5  6  7
    bool[] ba = [1, 1, 1, 1, 1, 1, 1, 1];
    bool[] bb = [0, 1, 1, 1, 1, 1, 1, 1]; 
    BitArray a; a.init(ba);
    BitArray b; b.init(bb);

    write("a ", cast(void[])a,": "); to_Bin(a);     // output contents
    write("b ", cast(void[])b,": "); to_Bin(b);     // output contents

    checkCmp(a, a, "a", "a");                       // output comparison
    checkCmp(a, b, "a", "b");                       // output comparison


    // PROBLEM SECTION:
    // BitPos:   0  1  2  3  4  5  6  7 |0  1  2  3  4  5  6  7
    bool[] bl = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];   
    bool[] bm = [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1];   
    BitArray l; l.init(bl);
    BitArray m; m.init(bm);
    
    write("l ", cast(void[])l,": "); to_Bin(l);
    write("m ", cast(void[])m,": "); to_Bin(m);

    checkCmp(l, l, "l", "l");   
    checkCmp(l, m, "l", "m");

}

================
which generates:
================

a [255, 0, 0, 0, 0, 0, 0, 0]: 11111111
b [254, 0, 0, 0, 0, 0, 0, 0]: 01111111
a = a
a > b
l [255, 255, 0, 0, 0, 0, 0, 0]: 1111111111111111
m [255, 253, 0, 0, 0, 0, 0, 0]: 1111111110111111
l = l
l = m                       <--------- WRONG

It seems unlikely that I could be using the comparison operators incorrectly in this case. Am I missing something?

Tracey