Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to store 0 in MSB of int datatype in C++?

#include<iostream>

using namespace std;

int main()


{

    int x = 0101;

    cout<<x;

    return 0;
}

The output I am getting is 101 but I want 0101 instead. what to do??

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int data type in c/c++ is not shown as BINARY for us, it is shown as a decimal.
So, your 0101 is actually in decimal 101, which in binary, is:
0000 0000 0000 0000 0000 0000 0110 0101

If you want to store binary 0101, it is in decimal: 5.
so,

#include<iostream>
using namespace std;
int main()
{
    int x = 5;
    cout<<x;
    return 0;
}

To output the number in binary form, you can apply an algorithm and/or either store it using bitset.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading