Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::bitset<N>::test

      From cppreference.com
      <cpp‎ |utility‎ |bitset
       
       
      Utilities library
       
       
      bool test(std::size_t pos)const;
      (constexpr since C++23)

      Returns the value of the bit at the positionpos (counting from 0).

      Unlikeoperator[], it performs a bounds check.

      Contents

      [edit]Parameters

      pos - position of the bit to return (counting from 0)

      [edit]Return value

      true if the requested bit is set,false otherwise.

      [edit]Exceptions

      Throwsstd::out_of_range ifpos does not correspond to a valid bit position.

      [edit]Example

      Run this code
      #include <bit>#include <bitset>#include <cassert>#include <iostream>#include <stdexcept> int main(){std::bitset<10> b1("1111010000"); std::size_t idx=0;while(idx< b1.size()&&!b1.test(idx))++idx; assert(static_cast<int>(idx)==std::countr_zero(b1.to_ulong())); if(idx< b1.size())std::cout<<"The first set bit is at index "<< idx<<'\n';elsestd::cout<<"no set bits\n"; try{std::bitset<0B10'1001'1010> bad;if(bad.test(bad.size()))std::cout<<"Expect unexpected!\n";}catch(std::out_of_rangeconst& ex){std::cout<<"Exception: "<< ex.what()<<'\n';}}

      Possible output:

      The first set bit is at index 4Exception: bitset::test: __position (which is 666) >= _Nb (which is 666)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2250C++98the behavior was undefined ifpos does
      not correspond to a valid bit position
      always throws an
      exception in this case

      [edit]See also

      accesses specific bit
      (public member function)[edit]
      (C++20)
      counts the number of1 bits in an unsigned integer
      (function template)[edit]
      checks if a number is an integral power of2
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/bitset/test&oldid=178326"

      [8]ページ先頭

      ©2009-2025 Movatter.jp