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
Program to find a Greatest String: C - Program: Write a program find largest string by c program. int main( int argc, char *argv[] ) {     if( argc         {

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing that

Explain Processing A Data File? Mainly data file applications needs that a data file be altered as it is being processed. For instance in an application involving the processin

6999066263304447777077766622337778 -----> message sent by the first smuggler. my name is robert---------> message decoded by the second smuggler. Where ‘0’ denotes the "space".


Aim: To implement a program to swap two numbers using reference arguments. Code: void swap(int *a, int *b) {             int temp;             temp=*a;

Scrape a site and save as csv Project Description: I want a programmer to create scraping software. I'll want every page scraped of name, job title, company, and url. There a

The #define Directive The #define directive explains a macro which is a text string represented by a name. Whenever the pre-processor finds this name in the program, it is repl

Project Description: I want an experienced programmer to program an RFID tag that will be read (maybe also written), and perform a call to a Filmmaker Pro 13 database. Skills

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4