It has been often asked why there is no formatter that will output an integer in binary. There are formatters that will output an integer in decimal and in hex. For example, printf("%lx",integernumber) will output integernumber in hex notation. For unknown reasons the founders of C++ and C did not include binary output capability.
The function itob.asm will accept a long integers as passed in parameter and return char array containing the 64 bits of that number. With this itob utility library program it is easy to output a long integer in binary.
Let intnumber be of type long (C or C++). Then intnumber can be outputted in binary using a statement such as
printf("%s",itol(intnumber)); or cout << itol(intnumber) << endl;
Posted July 31, 2023
Last tested by the author on August 10, 2023 using Xubuntu 22.4.
Sample execution on Xubuntu 22.4
activeprofessor@Margaret:~/FH/Tech/X86-program-development/Output-in-binary$ sh r.sh
rm: cannot remove '*.o': No such file or directory
rm: cannot remove '*.out': No such file or directory
Assemble itob.asm
Compile binarymain.c using gcc compiler standard 2017
Link object files using the gcc Linker standard 2017
Run the Arrays Program:
This main function will test the called function 'itob'
Please input an integer and press <enter>: 739485
You entered 739485
The binary expression is 0000000000000000000000000000000000000000000010110100100010011101
Enjoy your binary numbers
Script file terminated.
activeprofessor@Margaret:~/FH/Tech/X86-program-development/Output-in-binary$
Available for download
The conversion is in itob.asm. The main function provides a place to verify the correctness of itob.asm.