How to set specific bits in a char with C++

1 Answer

0 votes
#include <iostream>
#include <bitset>

int main() {
    unsigned char ch = 0; 
    ch |= (1 << 7);  // Set the 7th bit
    ch |= (1 << 3);  // Set the 3rd bit
    
    std::bitset<8> binary(ch);
    std::cout << binary << '\n';
    
    std::cout << "Value: " << static_cast<int>(ch) << std::endl; 
}



/*
run:

10001000
Value: 136

*/

 



answered Jul 29, 2025 by avibootz
...