Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
var t = Convert.ToString(n, 2)
.Reverse()
.TakeWhile(i => i == '0')
.Count();
Simple but not too elegant. Heavy use of LINQ.
int t = 0;
if(n != 0)
{
while((n & 1) == 0)
{
t++;
n >>= 1;
}
}
else
{
t = 8 * sizeof(int);
}
t = n.toRadixString(2)
.split('')
.reversed
.takeWhile((e) => e == '0')
.length;
trailingZeros(Num) ->
trailingZeros(Num, 0).
trailingZeros(Num, Count) when Num band 1 == 0 ->
trailingZeros(Num div 2, Count + 1);
trailingZeros(_, Count) -> Count.
t = trailz(n)
String p = ".+?(?<!0)(?=0+)",
s = toBinaryString(n);
int t = s.replaceAll(p, "").length();
String b = toBinaryString(n);
int i = b.lastIndexOf('1'), t;
t = i == -1 ? 0 : b.substring(++i).length();
function Trail(n: Integer): Integer;
var
Mask: Integer;
begin
T := 0;
while (T < SizeOf(Integer)*8) do
begin
Mask := 1 shl T;
if (n and Mask) <> 0 then Exit;
Inc(T);
end;
end;
begin
writeln(Trail(112),' (should be 4)');
end.
SizeOf(Integer) depends on the compiler mode and bitness of the OS you are compiling for.
$s = sprintf '%b', $n;
$n = length $s;
$t++ while !substr($s, --$n, 1) && $n >= 0;
Converts the integer in scalar variable $n into a string of zeros and ones. Extract characters from the end of the string until a non-zero is found. (A character 0 is treated as false, a 1 as true.) Stop when we reach the start of the string.
$t = sprintf('%b', $n) =~ /(0+)$/ ? length($1) : 0;
sprintf %b converts $n from an integer to a string of 1's and 0's. This is then matched (using the =~ operator) against a regular expression which looks for one or more zeroes at the end of the string. The parens around 0+ form a capture group which is accessible via the variable $1 and will contain the string of trailing 0's. If the match succeeds, then the length of $1 is computed and returned; if not, then a length of zero is returned.
t = bin(n)[::-1].find('1')
Works for n > 0. Integers in Python have no fixed bitsize, so t is undefined for n = 0.
b = len(s := format(n, 'b'))
t = b - len(s.rstrip('0'))
let t = n.trailing_zeros();