Difficulty: Easy · Puzzle 3 of 6 · Topic: String and Casting Puzzles
What do these boolean checks print?
module p3;
string a = "Reset";
string b = "reset";
initial begin
$display("eq=%0d", a == b);
$display("compare_eq0=%0d", a.compare(b) == 0);
$display("icompare_eq0=%0d", a.icompare(b) == 0);
end
endmodule`==` and `compare` are case-sensitive. `icompare` ignores case.
1) `a == b` is false because uppercase R differs from lowercase r.
2) `compare(...) == 0` is also false for same reason.
3) `icompare(...) == 0` is true because it performs case-insensitive comparison.Answer: eq=0, compare_eq0=0, icompare_eq0=1
Checked by running the code in Verilator 5; the printed output supports the answer above. Only icompare ignores case; Icarus 13 has no compare or icompare.
`==` and `compare` are case-sensitive while `icompare` is not; the three checks give 0, 0, 1.
Which method would you use to implement case-insensitive sort ordering? Model answer: `icompare`, which returns negative, zero or positive like `compare` (for "apple" vs "Banana", `icompare` gives a negative value because a sorts before b, while `compare` gives a positive one because lowercase sorts after uppercase; only the sign is defined, the magnitude is tool-specific: Verilator gave 31 for `compare` here).
I would say that equal-equal and compare are both case sensitive, so the capital R makes them different and gives 0 and 0. Icompare ignores case and returns 0 for a match, so that check is 1. The trap is mixing up the three, and remembering that compare returns zero for equal, not one. I would check which compare the testbench really needs.