Namespaces
Variants
Views
Actions

std::hash<std::bitset>

From cppreference.com
< cpp‎ | utility‎ | bitset
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

 
 
Defined in header <bitset>
template< std::size_t N >
struct hash<std::bitset<N>>;
(since C++11)

The template specialization of std::hash for std::bitset<N> allows users to obtain hashes of objects of type std::bitset<N>.

[edit] Example

The following code shows one possible output of a hash function used on several bitsets:

#include <bitset>
#include <functional>
#include <iostream>
 
int main()
{
    std::bitset<4> b1{0}, b2{42};
    std::bitset<8> b3{0}, b4{42};
 
    std::hash<std::bitset<4>> hash_fn4;
    std::hash<std::bitset<8>> hash_fn8;
    using bin64 = std::bitset<64>;
 
    std::cout << std::hex
              << bin64{hash_fn4(b1)} << " = " << hash_fn4(b1) << '\n'
              << bin64{hash_fn4(b2)} << " = " << hash_fn4(b2) << '\n'
              << bin64{hash_fn8(b3)} << " = " << hash_fn8(b3) << '\n'
              << bin64{hash_fn8(b4)} << " = " << hash_fn8(b4) << '\n';
}

Possible output:

0110110100001001111011100010011011010101100001100011011000011001 = 6d09ee26d5863619
1111111101011100010110100000111000111110100000111011100011110000 = ff5c5a0e3e83b8f0
0110110100001001111011100010011011010101100001100011011000011001 = 6d09ee26d5863619
0101110000011100011110011010111011100110010000110100110001001101 = 5c1c79aee6434c4d

[edit] See also

(C++11)
hash function object
(class template) [edit]