← Back to index
|
Original Bugzilla link
Bug 8382 – std.bitmanip opCmp bug or user error?
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2012-07-12T17:56:42Z
Last change time
2020-03-21T03:56:36Z
Assigned to
No Owner
Creator
Tracey
Comments
Comment #0
by tracey.freitas — 2012-07-12T17:56:42Z
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