4 # test the bit operators '&', '|', '^', '~', '<<', and '>>'
15 print ((0xdead & 0xbeef) == 0x9ead ? "ok 1\n" : "not ok 1\n");
16 print ((0xdead | 0xbeef) == 0xfeef ? "ok 2\n" : "not ok 2\n");
17 print ((0xdead ^ 0xbeef) == 0x6042 ? "ok 3\n" : "not ok 3\n");
18 print ((~0xdead & 0xbeef) == 0x2042 ? "ok 4\n" : "not ok 4\n");
21 print ((257 << 7) == 32896 ? "ok 5\n" : "not ok 5\n");
22 print ((33023 >> 7) == 257 ? "ok 6\n" : "not ok 6\n");
25 print ((~0 > 0 && do { use integer; ~0 } == -1)
26 ? "ok 7\n" : "not ok 7\n");
29 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
30 my $cusp = 1 << ($bits - 1);
32 print ((($cusp & -1) > 0 && do { use integer; $cusp & -1 } < 0)
33 ? "ok 8\n" : "not ok 8\n");
34 print ((($cusp | 1) > 0 && do { use integer; $cusp | 1 } < 0)
35 ? "ok 9\n" : "not ok 9\n");
36 print ((($cusp ^ 1) > 0 && do { use integer; $cusp ^ 1 } < 0)
37 ? "ok 10\n" : "not ok 10\n");
38 print (((1 << ($bits - 1)) == $cusp &&
39 do { use integer; 1 << ($bits - 1) } == -$cusp)
40 ? "ok 11\n" : "not ok 11\n");
41 print ((($cusp >> 1) == ($cusp / 2) &&
42 do { use integer; abs($cusp >> 1) } == ($cusp / 2))
43 ? "ok 12\n" : "not ok 12\n");
45 $Aaz = chr(ord("A") & ord("z"));
46 $Aoz = chr(ord("A") | ord("z"));
47 $Axz = chr(ord("A") ^ ord("z"));
50 print (("AAAAA" & "zzzzz") eq ($Aaz x 5) ? "ok 13\n" : "not ok 13\n");
51 print (("AAAAA" | "zzzzz") eq ($Aoz x 5) ? "ok 14\n" : "not ok 14\n");
52 print (("AAAAA" ^ "zzzzz") eq ($Axz x 5) ? "ok 15\n" : "not ok 15\n");
59 print (($foo & $bar) eq ($Aaz x 75 ) ? "ok 16\n" : "not ok 16\n");
61 print (($foo | $bar) eq ($Aoz x 75 . $zap) ? "ok 17\n" : "not ok 17\n");
63 print (($foo ^ $bar) eq ($Axz x 75 . $zap) ? "ok 18\n" : "not ok 18\n");
66 print "ok \xFF\xFF\n" & "ok 19\n";
67 print "ok 20\n" | "ok \0\0\n";
68 print "o\000 \0001\000" ^ "\000k\0002\000\n";
71 print "ok \x{FF}\x{FF}\n" & "ok 22\n";
72 print "ok 23\n" | "ok \x{0}\x{0}\n";
73 print "o\x{0} \x{0}4\x{0}" ^ "\x{0}k\x{0}2\x{0}\n";
76 print "ok 25\n" if sprintf("%vd", v4095 & v801) eq 801;
77 print "ok 26\n" if sprintf("%vd", v4095 | v801) eq 4095;
78 print "ok 27\n" if sprintf("%vd", v4095 ^ v801) eq 3294;
81 print "ok 28\n" if sprintf("%vd", v4095.801.4095 & v801.4095) eq '801.801';
82 print "ok 29\n" if sprintf("%vd", v4095.801.4095 | v801.4095) eq '4095.4095.4095';
83 print "ok 30\n" if sprintf("%vd", v801.4095 ^ v4095.801.4095) eq '3294.3294.4095';
85 print "ok 31\n" if sprintf("%vd", v120.300 & v200.400) eq '72.256';
86 print "ok 32\n" if sprintf("%vd", v120.300 | v200.400) eq '248.444';
87 print "ok 33\n" if sprintf("%vd", v120.300 ^ v200.400) eq '176.188';
92 print "ok 34\n" if sprintf("%vd", $a) eq '176.188';
96 print "ok 35\n" if sprintf("%vd", $a) eq '248.444';
102 my $Is_EBCDIC = (ord('A') == 193) ? 1 : 0;
106 for (0x100...0xFFF) {
109 push @not36, sprintf("%#03X", $_)
110 if $a ne chr(~$_) or length($a) != 1;
113 push @not36, sprintf("%#03X", $_)
114 if $a ne chr(~$_) or length($a) != 1 or ~$a ne chr($_);
118 print "# test 36 failed\n";
125 for my $i (0xEEE...0xF00) {
126 for my $j (0x0..0x120) {
127 $a = ~(chr ($i) . chr $j);
129 push @not37, sprintf("%#03X %#03X", $i, $j)
130 if $a ne chr(~$i).chr(~$j) or
134 push @not37, sprintf("%#03X %#03X", $i, $j)
135 if $a ne chr(~$i).chr(~$j) or
137 ~$a ne chr($i).chr($j);
142 print "# test 37 failed\n";
147 print "not " unless ~chr(~0) eq "\0" or $Is_EBCDIC;
152 for my $i (0x100..0x120) {
153 for my $j (0x100...0x120) {
154 push @not39, sprintf("%#03X %#03X", $i, $j)
155 if ~(chr($i)|chr($j)) ne (~chr($i)&~chr($j));
159 print "# test 39 failed\n";
166 for my $i (0x100..0x120) {
167 for my $j (0x100...0x120) {
168 push @not40, sprintf("%#03X %#03X", $i, $j)
169 if ~(chr($i)&chr($j)) ne (~chr($i)|~chr($j));
173 print "# test 40 failed\n";
178 # More variations on 19 and 22.
179 print "ok \xFF\x{FF}\n" & "ok 41\n";
180 print "ok \x{FF}\xFF\n" & "ok 42\n";
182 # Tests to see if you really can do casts negative floats to unsigned properly
184 print ((~ $neg1 == 0) ? "ok 43\n" : "not ok 43\n");
186 print ((~ $neg7 == 6) ? "ok 44\n" : "not ok 44\n");