Namespaces
Variants

cpp/utility/bitset/operator at: Difference between revisions

From cppreference.com
the standard is a little vague, but I think that operator[] throws if the arg is invalid
Space Mission (talk | contribs)
m {{p}} (for “paragraph”) gives lesser V offset.
 
(22 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{cpp/utility/bitset/title | operator[]}}
{{cpp/utility/bitset/title|operator[]}}
{{cpp/utility/bitset/sidebar}}
{{cpp/utility/bitset/}}
{{ddcl list begin}}
{{begin}}
{{ddcl list item | num=1 |
{{|num=1|
bool operator[]( size_t pos ) const;
bool operator[]( size_t pos ) const;
}}
}}
{{ddcl list item | num=2 |
{{|num=2|
reference operator[]( size_t pos );
reference operator[]( size_t pos );
}}
}}
{{ddcl list end}}
{{end}}


Accesses the bit at position {{tt|pos}}. The first version returns the value of the bit, the second version returns an object of type {{cpp|std::bitset::reference}} that allows modification of the value.
Accesses the bit at position {{|pos}}.
the value of the bit
an object of type {{|std::bitset::reference}} that allows modification of the value.


Throws {{cpp|std::out_of_range}} if {{tt|pos}} does not correspond to a valid bit position.
{{cpp|}} {{|}}


===Parameters===
===Parameters===
{{param list begin}}
{{begin}}
{{param list item | pos | position of the bit to return}}
{{|pos|position of the bit to return}}
{{param list end}}
{{end}}


===Return value===
===Return value===
1) the value of the requested bit
1value of the requested bit


2) an object of type {{cpp|std::bitset::reference}} with the value of the bit.
2object of type {{|std::bitset::reference}}the bit
 
.


===Example===
===Example===
{{example cpp
{{example
| code=
|code=
#include <iostream>
#include <iostream>
#include <bitset>


int main()  
int main()
{
{
     std::bitset<8> b1(42);
     std::bitset<8> b142
     for (int i = 0; i < b1.size(); ++i) {
     for (i = 0; i < b1.size(); ++i)
         std::cout << "b1[" << i << "]: " << b1[i] << '\n';
         std::cout << "b1[" << i << "]: " << b1[i] << '\n';
     }
    
     return 0;
     0;
}
}
| output=
|output=
b1[0]: 0
b1[0]: 0
b1[1]: 1
b1[1]: 1
Line 47: Line 56:
b1[6]: 0
b1[6]: 0
b1[7]: 0
b1[7]: 0
}}
}}




===See also===
===See also===


{{dcl list begin}}
{{|}}
{{dcl list template | cpp/utility/bitset/dcl list test}}
{{dcl list end}}

Latest revision as of 04:54, 28 March 2025

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
bool operator[]( std::size_t pos ) const;
(1) (constexpr since C++11)
reference operator[]( std::size_t pos );
(2) (constexpr since C++23)

Accesses the bit at position pos.

1) Returns the value of the bit.
2) Returns an object of type std::bitset::reference that allows modification of the value.

If pos < size() is false, the behavior is undefined.

(until C++26)

If pos < size() is false:

(since C++26)

Parameters

pos - position of the bit to return

Return value

1) The value of the requested bit.
2) An object of type std::bitset::reference, which allows writing to the requested bit.

Exceptions

Throws nothing.

Example

#include <bitset>
#include <cstddef>
#include <iostream>

int main()
{
    std::bitset<8> b1{0b00101010}; // binary literal for 42
    
    for (std::size_t i = 0; i < b1.size(); ++i)
        std::cout << "b1[" << i << "]: " << b1[i] << '\n';
    b1[0] = true; // modifies the first bit through bitset::reference
    
    std::cout << "After setting bit 0, b1 holds " << b1 << '\n';
}

Output:

b1[0]: 0
b1[1]: 1
b1[2]: 0
b1[3]: 1
b1[4]: 0
b1[5]: 1
b1[6]: 0
b1[7]: 0
After setting bit 0, b1 holds 00101011

Defect reports

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

DR Applied to Behavior as published Correct behavior
LWG 11 C++98 1. the description was missing in the C++ standard
2. there was only the non-const overload
1. description added
2. added the const overload
LWG 907 C++98 the behavior of reading the bit at pos was equivalent
to that of test(pos), but test() may throw exceptions
avoids mentioning test()

See also

accesses specific bit
(public member function) [edit]