Hi, i right now have a question for my CS Course. is about convert decimal to bi
ID: 3623231 • Letter: H
Question
Hi, i right now have a question for my CS Course. is about convert decimal to binary. we need to modify the code that provide by our professor.here is the code that provide by my professor:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(int argc, char *argv[])
{
int x;
// Start with the value 4, whose least significant byte
// is 00000100 in binary
x = 4;
cout << "x = " << x << endl;
// Now we'll "bitwise or" in the value for 2, which is
// 00000010 in binary. The result is 00000110, or 6 (decimal)
x = x | 2;
cout << "x = " << x << endl;
// The notation "x |= y" is equivalent to "x = x | y"
// in C++ (and C). We'll bitwise or a 16 in there, to produce
// 00010110 binary, or 22 decimal. Notice that if you bitwise
// or powers of 2 (4, 2, 16 so far), this is the same as adding
// them.
x |= 16;
cout << "x = " << x << endl;
// Notice that when you bitwise or a 16 again, the result is
// still 22 (this is an "or", not an xor)
x |= 16;
cout << "x = " << x << endl;
// Let's work with the "bitwise and" operator
// x &= 21 will show which bits x and 20 BOTH have set in their
// binary representation.
// x's current value is 22, which in binary is 00010110
// 21 in binary is 00010101
// the bits they have in common are 00010100
// the decimal value of 00010100 is 4 + 16 = 20
x &= 21;
cout << "x = " << x << endl;
// Notice (for your Lab) that if you bitwise & with a value
// that is a power of two, you can check if that particular
// bit is set.
// Let's see if 427 has the "twos" bit set
if (427 & 2)
cout << "427 does have the 2's bit set" << endl;
else
cout << "427 does NOT have the 2's bit set" << endl;
// Notice above that we're using the fact that any non-zero value
// for the expression (427 & 2) will make it true. Equivalently,
// we could have written "if ((427 & 2) == 2)"
// Let's do that to see if the ones bit is set:
if ((427 & 4) == 4)
cout << "427 does have the 4's bit set" << endl;
else
cout << "427 does NOT have the 4's bit set" << endl;
// Finally, let's see how the shift operator works. Shifting
// moves bits one way or the other. Let's start with 1 and shift
// in from the right, 31 times, to list the powers of two.
int i;
int counter;
counter = 1;
for (i=1; i<32; i++) {
cout << "2^" << i << " = " << counter << endl;
counter <<= 1;
}
}
Explanation / Answer
please rate - thanks
you didn't say what to modify it to
this converts decimal to binary using shifting and anding
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int num,shifted,i;
cout<<"Enter integer you want to convert (0 when done): ";
cin>>num;
while(num!=0)
{cout<<"bin: ";
for(i=31;i>=0;i--)
{shifted=num; //don't destroy original number
shifted=shifted>>i; //shift to the right
shifted=shifted&1; //use mask to get right most bit
cout<<shifted; //print the digit
}
cout<<endl<<endl; //when done with all digits skip 2 lines
cout<<"Enter integer you want to convert (0 when done): ";
cin>>num;
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.