Linked Questions
32 questions linked to/from How to print (using cout) a number in binary form?
0 votes
5 answers
276 views
simple change of a character to its bit representation [duplicate]
Why am I getting an error? It looks pretty straightforward to me. Also, is this the best method for doing what I'm trying to do? #include <iostream> #include <string> int main() { char ...
0 votes
2 answers
284 views
How can I print to my file in binary instead of hexadecimal [duplicate]
I am trying to write a file that outputs every possible 16 bit number. I am getting the output in 16 digit hex instead of 16 digit binary. How can I get it in binary. Thank you FILE * file = fopen("...
0 votes
0 answers
226 views
How to print the binary value of negative numbers? [duplicate]
I have been trying to find the binary representation of negative numbers. As per my knowledge the negative numbers are stored as a 2's complement in the computer. And, the MSB is 1, indicating that it ...
0 votes
0 answers
59 views
Print out every bit of variable like 0 or 1 in byte blocks [duplicate]
I have a double variable. Is it possible to output every bit of this variable using char*(char pointer) ? So, char pointer would be char* p = (char*)&myVar; at the beginning and then it will be ...
4 votes
5 answers
13k views
How to insert a character every N characters in a string in C++
How can I insert a character into a string exactly after 1 character? I need to insert '|' into the string after every other character. In other words (C++): "Tokens all around!" Turns into: "T|o|k|...
3 votes
3 answers
22k views
C++ reading binary files
I want to understand how does reading binary files work in C++. My code: int main() { ifstream ifd("input.png",ios::binary |ios::ate); int size = ifd.tellg(); ifd.seekg(0, ios::beg); ...
2 votes
2 answers
5k views
C++ bitwise complement in unsigned integer returns negative values
I am just trying to do the bitwise complement in C++ with ~ operator: For example: NOT 0101 -------- 1010 So in the following code, I was expecting to get 1010 but I am getting negative numbers. ...
4 votes
2 answers
2k views
How do I write binary data to a file in Modern C++?
Writing binary data to a file in C is simple: use fwrite, passing the address of the object you want to write and the size of the object. Is there something more "correct" for Modern C++ or should I ...
-2 votes
1 answer
3k views
C++ How to output int as 32-bit binary?
I want to output an int in 32-bit binary format. Is looping and shifting my only option?
1 vote
1 answer
2k views
How to print binary representation of a long double as in computer memory?
I have to print the binary representation of a long double number for some reasons. I want to see the exact format as it remains in the computer memory. I went through the following questions where ...
0 votes
4 answers
4k views
Printing integers as a set of 4 bytes arranged in little endian?
I have an array of 256 unsigned integers called frequencies[256] (one integer for each ascii value). My goal is to read through an input and for each character i increment the integer in the array ...
0 votes
2 answers
2k views
std::make_unsigned not giving expected results
I have something like this: template<class T> class SomeClass { public: typedef std::make_unsigned<T> unsigned_t; unsigned_t maxBinBitRep_ { - 1 }; }; int main() { // ...
0 votes
2 answers
3k views
adding a digit at the end of a number, (not summing) in C++
I already tried looking for this and they gave me to just do along the lines of: int x = 1; while (x != 11) { x = x * 10 + (x+1); } cout<<x; output: 12345678910 While this is good and all ...
1 vote
2 answers
2k views
Bit shifting a half-float into a float
I have no choice but to read in 2 bytes that make up a half-float. I would like to work with this in the form of a 4 byte float. Ive done some research and the only thing I can come up with is bit ...
-1 votes
2 answers
3k views
How to convert an unsigned integer into 32 bit binary representation
This is what I have for my conversion function so far It takes an unsigned integer as a parameter. It should give the result like outputBinary(1) //=> 0000 0000 0000 0000 0000 0000 0000 0001 ...