Write down a function to display an integer in binary format, C/C++ Programming

A: void displayBits( unsigned value )

{

const int SHIFT = 8 * sizeof( unsigned ) - 1;

const unsigned MASK = 1<< SHIFT;

cout << setw(10 ) << value << " = ";

for ( unsigned i = 1; i <= SHIFT + 1; i++ )

{

cout << ( value & MASK ? '1' : '0' );

value <<= 1;

if ( i % 8 == 0 ) // output a space after bits cout << ' ';

}

cout << endl;

}

You may do the same by divide by 2, till the number is greator than 0. However you ought to use stack to print it in reverse order.

 

 

Posted Date: 3/20/2013 8:18:16 AM | Location : United States







Related Discussions:- Write down a function to display an integer in binary format, Assignment Help, Ask Question on Write down a function to display an integer in binary format, Get Answer, Expert's Help, Write down a function to display an integer in binary format Discussions

Write discussion on Write down a function to display an integer in binary format
Your posts are moderated
Related Questions
how do you declare an integer variable

For this program, you are going to modify your previous program (program 3) so that it will now have a menu to see if the user wants to read the input from a file or interactively.

Problem : (a) Stacks can be implemented using arrays. Write down the following C or C++ function to (i) create a stack (ii) add an element to a stack (push). (b) Expl


main() { int a[5]={1,3,6,7,0}; int *b; b=&a[2]; } The value of b[-1] is


for object, malloc allocates memory in heap however doesn't invoke object's constructor to initialize the object. new allocates memory & also invokes constructor to initialize the


A virtual function permits derived classes to replace the implementation given by the base class. The compiler makes sure the replacement is always known as whenever the object in

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes