0
#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

Vikram
  • 41
  • 5
  • The values of both num and n will be 0, that's why the end result will be 0 too – Asesh Aug 25 '17 at 03:36
  • 3
    Stop doing this: `#include` and include the correct headers, i.e. `` and ``. – PaulMcKenzie Aug 25 '17 at 03:40
  • 1
    `01000` is not a 5-bit number, it's 0x200 or 512 in decimal [What does it mean when a numeric constant in C/C++ is prefixed with a 0?](https://stackoverflow.com/q/6365565/995714), [printf with “%d” of numbers starting with 0 (ex “0102”) giving unexpected answer](https://stackoverflow.com/q/19652583/995714) – phuclv Aug 25 '17 at 03:41

3 Answers3

1

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

phuclv
  • 37,963
  • 15
  • 156
  • 475
0

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;
Asesh
  • 3,186
  • 2
  • 21
  • 31
0

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;
}
WindyFields
  • 2,697
  • 1
  • 18
  • 21