Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,938 questions

51,875 answers

573 users

How to perform binary shift right using bitset in C++

1 Answer

0 votes
#include <bitset>
#include <iostream>
 
int main()
{
    std::bitset<8> b { 0b01101110 };
    std::cout << b << " (first value)\n";
 
    // bitset& operator >>= (std::size_t pos);
    
    for (; b.any(); b >>= 1) {
        while ( !b.test(0)) {
            b >>= 1;
        }
        std::cout << b << '\n';
    }
 
    std::cout << b << " (last value)\n";
}


/*
run:

01101110 (initial value)
00110111
00011011
00001101
00000011
00000001
00000000 (final value)

*/

 



answered Oct 13, 2024 by avibootz
...