how to output an int in binary?
Asked Answered
A

6

18
int x = 5;
cout<<(char)x;

the code above outputs an int x in raw binary, but only 1 byte. what I need it to do is output the x as 4-bytes in binary, because in my code, x can be anywhere between 0 and 2^32-1, since

cout<<(int)x;

doesn't do the trick, how would I do it?

Apples answered 17/7, 2010 at 1:1 Comment(2)
What does it matter? He posted what he was trying to do so far and why his code wasn't working and asked a clear question.Storz
That's not likely to work well with cout, because you don't have control over how it's opened. If you want to do binary output, open your own stream and include the ios_base::binary flag in your openmmode argument.Chivalric
R
12

You can use the std::ostream::write() member function:

std::cout.write(reinterpret_cast<const char*>(&x), sizeof x);

Note that you would usually want to do this with a stream that has been opened in binary mode.

Roomy answered 17/7, 2010 at 1:5 Comment(0)
P
38

A bit late, but, as Katy shows in her blog, this might be an elegant solution:

#include <bitset>
#include <iostream>

int main(){
  int x=5;
  std::cout<<std::bitset<32>(x)<<std::endl;
}

taken from: https://katyscode.wordpress.com/2012/05/12/printing-numbers-in-binary-format-in-c/

Payer answered 14/3, 2016 at 19:42 Comment(3)
Probably the most reasonable answer. It does sort of assume that we're talking about a 32-bit int, but we're making such assumptions in almost every other answer too.Simonides
@Simonides You can do something like std::bitset<sizeof(int)*8> (or std::bitset<sizeof(decltype(x))*8>) to avoid those assumptions.Bharal
I think it's important to note that this answer is not answering the question.Foliolate
R
12

You can use the std::ostream::write() member function:

std::cout.write(reinterpret_cast<const char*>(&x), sizeof x);

Note that you would usually want to do this with a stream that has been opened in binary mode.

Roomy answered 17/7, 2010 at 1:5 Comment(0)
C
4

Try:

int x = 5;
std::cout.write(reinterpret_cast<const char*>(&x),sizeof(x));

Note: That writting data in binary format is non portable.
If you want to read it on an alternative machine you need to either have exactly the same architecture or you need to standardise the format and make sure all machines use the standard format.

If you want to write binary the easiest way to standardise the format is to convert data to network format (there is a set of functions for that htonl() <--> ntohl() etc)

int x = 5;
u_long  transport = htonl(x);
std::cout.write(reinterpret_cast<const char*>(&transport), sizeof(u_long));

But the most transportable format is to just convert to text.

std::cout << x;
Complexity answered 17/7, 2010 at 1:6 Comment(6)
where are htonl and ntohl defined? I can't find them in the reference.Gronseth
@RafaelCamposNunes: Its not part of the C++ standard. Its part of the POSIX standard. linux.die.net/man/3/htonlComplexity
Oh, I see. So it's not very portable.Gronseth
@RafaelCamposNunes It means its not part of the language standard (as the language is hardware agnostic (so it can't be part of that standard)). Its part of the platform standard, as a platform has to know about the hardware details. All modern OS support the POSIX standard. You only get into issues if you are using micro-controllers or something without an OS.Complexity
Modern versions of Windows does not support POSIX as far as I know.Gronseth
@RafaelCamposNunes I think you will find this part of the POSIX standard (not sure what section it is) is implemented everywhere (its too easy).Complexity
N
2

and what about this?

int x = 5;
cout<<(char) ((0xff000000 & x) >> 24);
cout<<(char) ((0x00ff0000 & x) >> 16);
cout<<(char) ((0x0000ff00 & x) >> 8);
cout<<(char) (0x000000ff & x);

Nd answered 17/7, 2010 at 2:8 Comment(1)
Very possibly this is more on the right track than the other answers -- the original question doesn't specify "write in binary" how. If it's "however it happens to be in memory", that's one thing, but if you're trying to be compatible with anything else, then you might want to (for instance) always write a 32-bit long in network byte order, as this answer does (modulo some compatibility issues)Imhoff
R
1

A couple of hints.

First, to be between 0 and 2^32 - 1 you'll need an unsigned four-byte int.

Second, the four bytes starting at the address of x (&x) already have the bytes you want.

Does that help?

Riding answered 17/7, 2010 at 1:9 Comment(0)
R
1

Starting with C++20, one can also use std::format and the binary format specifier b:

#include <format>
#include <iostream>

int x = 5;
std::cout << std::format("In binary: {:b}\n", x);    // 101
std::cout << std::format("In binary: {:08b}\n", x);  // 00000101
std::cout << std::format("In binary: {:#08b}\n", x); // 0b000101

Godbolt link.

Razorbill answered 28/12, 2022 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.