| Bytes | Lang | Time | Link |
|---|---|---|---|
| 004 | Uiua | 241115T173050Z | nyxbird |
| 029 | AWK | 241115T172705Z | xrs |
| 006 | Japt | 241115T115510Z | Shaggy |
| 087 | C | 110204T115349Z | fat0ss |
| 036 | SmileBASIC | 170216T214832Z | 12Me21 |
| 009 | J | 160609T002334Z | Leaky Nu |
| nan | 110204T100642Z | Daniel | |
| 029 | Racket scheme | 160609T131828Z | Simon Ze |
| 016 | Golfscript | 110207T052307Z | gnibbler |
| 031 | Perl | 110206T095812Z | J B |
AWK, 29 bytes
{for(x=2^$2;$1>x;)$1-=x}$0=$1
It's reasonably fast when N and M aren't too far apart :-p
82 bytes, but fast
{x=1;for(y=2^$2;x<=$1;)x=lshift(x,1)
for(;x>y;$1>=x&&$1-=x)x=rshift(x, 1);print$1}
Japt, 6 bytes
¢tVn)Í
¢tVn)Í :Implicit input of integers U=N & V=M
¢ :Convert U to binary string
t :Substring of length
Vn : -V (i.e., the last V digits)
) :Convert to decimal
4 bytes
Using modulo & exponentiation methods, not operators.
u2pV
u2pV :Implicit input of integers U=N & V=M
u :U modulo
2pV : 2 raised to the power of V
C, 87 bytes
scanf("%d", &T);
while(T--){
scanf("%d,%d", &N,&M);
printf("%d",N&~(~0 << M));
}
SmileBASIC, 36 characters
INPUT N,M?N AND(1<<M)-1
There's basically only one solution to this...
J, 15 12 9 bytes
6 bytes thanks to @miles.
#.@#:~#&2
Previous 12-byte version
#.@({.~-)~#:
Usage
>> f =: #.@({.~-)~#:
>> 6 f 150
<< 22
Where >> is STDIN and << is STDOUT.
Explanation
#: converts n to base 2.
({.~-)~ then takes the last m digits from it
#. converts it back to base 2.
Previous 15-byte version:
2#.(0-[){.[:#:]
Usage
>> f =: 2#.(0-[){.[:#:]
>> 6 f 150
<< 22
Where >> is STDIN and << is STDOUT.
Explanation
[:#:] converts n to base 2.
(0-[){. then takes the last m digits from it
2#. converts it back to base 2.
Java
With whitespace to make readable:
long F(long M, long N) {
M = 2<<M;
while(N>=M) N-=M;
return N;
}
Racket (scheme) 29 bytes
Kinda cheating because racket doesn't even have the % operator:
(λ(n m)(modulo n(expt 2 m)))
Golfscript - 16 chars
~]1>2/{~2\?(&n}/
For a single pair or numbers this would sufficient
~2\?(&
By the way, the / does not stand for division here :)
Perl, 31
<>;/ /,say($`&~(-1<<$'))while<>
Perl 5.10 or later, run with perl -E '<code here>'