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
Nested Classes Many a times, it becomes essential to have a class contain properties of two other classes. One way is to explain a class within another - that is a class with m

i need to do my home work

How do you know that your class requires a virtual destructor? A: If at least your class has one virtual function, you must make a destructor for this class virtual. It will let

how to search, display all data and delete data

I have a Matlab assignment and I have the assignment description+ algorithm + and the output sample. the files are on a dropbox folder and you can download them from the link below

Q. Explain that how do we implement two stacks in one array A[1..n] in such a way that neither the stack overflows unless the total number of elements in both stacks together is n.

Define the Modulo Division Operator in c language? The C provides one more arithmetic operator % called as modulo division operator and this operator yields the remainder of an

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 task is to convert S to palindrome

develop an algorithm using pseudocode for computing cos(x) and sin(x). use a sentinel controlled while loop. use the series definition of e^+-jx

To write a program to use different levels of array of pointer to pointer & display the output as 1 1 1 2 2 2 3 3 3