#include<bits/stdc++.h>
using namespace std;
int main(){
bitset<5> num=01000;
bitset<5> n=00000;
bitset<5> result;
result=(n|num);
cout<<result;
}
Answer should be 1000 but it shows 00000
#include<bits/stdc++.h>
using namespace std;
int main(){
bitset<5> num=01000;
bitset<5> n=00000;
bitset<5> result;
result=(n|num);
cout<<result;
}
Answer should be 1000 but it shows 00000
01000
is an octal integer literal whose value is 512 with the 5 least significant bits being 0. Same to 00000
Hence both num
and n
will be 0
To set the bitset to 01000
binary you can use
bitset<5> num("01000")
bitset<5> num(0b01000)
using C++14's binary integer literalbitset<5> num(0x10)
If you want to assign binary numbers then you can enclose them double quotes:
std::string binary_number = "1000";
std::bitset<5> num(binary_number);
std::bitset<5> n("0");
std::bitset<5> result;
result = (n | num);
std::cout << result;
but if you don't want to use enclose them in double quotes then you can do something like this:
std::bitset<5> num = 8;
std::bitset<5> n = 0;
std::bitset<5> result;
result = (n | num);
std::cout << result;
Binary literals have dedicated notion (since C++14): 0b01000
not 01000
.
#include <bitset>
int main(int argc, char* argv[])
{
std::bitset<5> num = 0b01000;
std::bitset<5> n = 0b00000;
std::bitset<5> result;
result = (n | num);
std::cout << result << std::endl; // -> 01000
return 0;
}