diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2021-07-12 15:45:10 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2021-07-12 15:45:10 +0000 |
| commit | cc1e0f89c996e9486d40a8968104169882fd1bab (patch) | |
| tree | fd3287207abf3df3ebbb107f1afcf296cf3fff87 | |
| parent | 5ecd146deeffbad7fce1a07918da4b5c140fd05d (diff) | |
| download | perlweeklychallenge-club-cc1e0f89c996e9486d40a8968104169882fd1bab.tar.gz perlweeklychallenge-club-cc1e0f89c996e9486d40a8968104169882fd1bab.tar.bz2 perlweeklychallenge-club-cc1e0f89c996e9486d40a8968104169882fd1bab.zip | |
Revised task 1
(back to the schoolbooks and make it work for larger integer values)
| -rwxr-xr-x | challenge-121/perlboy1967/perl/ch-1.pl | 6 |
1 files changed, 2 insertions, 4 deletions
diff --git a/challenge-121/perlboy1967/perl/ch-1.pl b/challenge-121/perlboy1967/perl/ch-1.pl index 8ac1ad92bf..81d9837877 100755 --- a/challenge-121/perlboy1967/perl/ch-1.pl +++ b/challenge-121/perlboy1967/perl/ch-1.pl @@ -20,11 +20,11 @@ my $tests = [ [ 12, 3, 8], [ 18, 4, 26], [ 0b10101010, 5, 0b10111010 ], + [ 0b111111111111, 8, 0b111101111111 ], ]; foreach my $t (@$tests) { printf "%s\n", join(',', @$t) unless is(invertBit($t->[0],$t->[1]), $t->[2]); - print "------------\n"; } done_testing(); @@ -32,7 +32,5 @@ done_testing(); sub invertBit($$) { my ($m,$n) = @_; - # upper part + modified bit + lower part - return (((($m >> $n) << 1 ) + ((($m >> ($n - 1)) & 1 ) ^ 1)) << ($n - 1)) + - ($m & (0xff >> (8 - $n + 1))); + return $m ^ (1 << ($n-1)); } |
