rcpl

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ruthen71/rcpl

:heavy_check_mark: Lowbit
(misc/lowbit.hpp)

lowbit

int lowbit(T x)

最下位に立っているビットの位置を返します。

$x = 0$ のときは $-1$ を返します。

Depends on

Required by

Verified with

Code

#pragma once

#include "./countr_zero.hpp"

// lowbit
// (000, 001, 010, 011, 100) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : countr_zero(x)); }
int lowbit(unsigned int x) { return (x == 0 ? -1 : countr_zero(x)); }
int lowbit(long long int x) { return (x == 0 ? -1 : countr_zero(x)); }
int lowbit(unsigned long long int x) { return (x == 0 ? -1 : countr_zero(x)); }
#line 2 "misc/lowbit.hpp"

#line 2 "misc/countr_zero.hpp"

#if __cplusplus >= 202002L
#include <bit>
#endif

// countr_zero
// (000, 001, 010, 011, 100) -> (32, 0, 1, 0, 2)
#if __cplusplus >= 202002L
using std::countr_zero;
#else
int countr_zero(unsigned int x) {
    return x == 0 ? 32 : __builtin_ctz(x);
}
int countr_zero(unsigned long long int x) {
    return x == 0 ? 64 : __builtin_ctzll(x);
}
#endif
int countr_zero(int x) { return countr_zero((unsigned int)(x)); }
int countr_zero(long long int x) {
    return countr_zero((unsigned long long int)(x));
}
#line 4 "misc/lowbit.hpp"

// lowbit
// (000, 001, 010, 011, 100) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : countr_zero(x)); }
int lowbit(unsigned int x) { return (x == 0 ? -1 : countr_zero(x)); }
int lowbit(long long int x) { return (x == 0 ? -1 : countr_zero(x)); }
int lowbit(unsigned long long int x) { return (x == 0 ? -1 : countr_zero(x)); }
Back to top page