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
Develop G-code generating software Project Description: I'm presently using Slicer on a polymer jetting 3d printer. The toolpaths were thought out for FDM printers and thus a

Control structures The control structures appear in both structured programming languages as well as object oriented programming languages.  The three constructs used are: i)

Project Description: 1) Develop banking software 2) Payment Gateway System There is some other system available for development. I'm willing to show long-term opportunity

For your class to work properly, you'll need to define appropriate constructors, extract and insert operators, and of course arithmetic operators. (If you wanted to use it as a gen

Question A bank normally updates it's clients accounts at the end of each month.Of the two types of bank accounts:savings and checking, a client must maintain a minimum balance

Problem : Change to palindrome A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your ta

Project Description: Seeking a Voting Bot Script / Program Skills required are ASP, Javascript, C Programming, PHP, Software Architecture

Write a ‘C’ program to accept any 3 digit integer number from the keyboard and display the word equivalent representation of the given number.

compute algorithm to calculate simple interest

Dynamic Initialization of objects: It is initializing the objects by passing the valued to the constructor from the user input or other means.   Through cin operator a value