Decimal Division by left shift

Felix picture Felix · Aug 28, 2013 · Viewed 9.4k times · Source

I have been given a question to convert base from 10 to 2 without using division(/) and modules(%),so I came up with solution of using bitwise AND(&)and right shift(>>) operators.

so I start to learn what these two operators exactly do but still for some questions I could not find answer or understand the logic behind.

If I understand correctly division works according the digits place value,in decimal and binary both . when we divide the number by 10 or 2 ,we shift the place value by one place to right in both ,which this will result in division by 10 in decimal and by two in binary.

X=120 (in base of ten) if X>>1 we will have X=12 (division by 10)

Y=1000 (in base of two) if Y>>1 we will have X=100 (division by 2)

but when I use this piece of code:

#include<iostream>
using namespace std ;

int main()
{
    int a,b=1;
    cout <<"enter an integer"<<endl;
    cin>> a;
    cout<<(a & b)<<endl;
    a=a>>1;
    cout<<a;
    cout<<endl;
    system("pause");
    return 0 ;
}

I get comfused cause in my mind it was like this

a=120 (in base of ten) if X>>1 we will have X=12 (division by 10)

but the result was this

a=120 (in base of ten) if X>>1 we have X=60 (division by 2!!)

I do not understand two main point about the result:

first: if this operator(>>) just shift the place value of the digits in code and don't change the base of the number(10) it should produce another result(12) than we can see in result of code (which it is 60).why we can see this result(60) but not 12?

second:if it does binary left shift (which it seems like this for me),does it change the decimal to binary at first by IDE or not?

and about the bitwise AND if it is logical gate(which it seems it is) :

1.How can we put other value except 0 and 1 and steel have answer?

2.According Bitwise AND rules

Y&1=Y

then it should be 120 but the result of code is 1. what is explanation for this?

3.how it can generate the reminder(according which mathematical operations and logic)?

Answer

Dietmar K&#252;hl picture Dietmar Kühl · Aug 28, 2013

The shift operators in C++ always use base 2. That is, x >> 1 shifts the value x by one binary digits. Note, however, that it isn't a good idea to shift signed integers as their value gets easily unspecified: When playing with bit logic, you always want to use unsigned integers, e.g., unsigned int or unsigned long. The conversion from your decimal values to the internal representation is done by the input operation which, BTW, needs to be checked for success:

if (std::cin >> a) {
     ...
}
else {
    std::cerr << "ERROR: failed to read value a\n";
}

The other binary operation (& for and, | for or, ^ for _xor, and ~ for invert) operate on the individual bits. For example, 7u & 13u yields 5u. To get the remainder of a division by a power of 2 you just use and prior to the division with a suitable bitmask.

BTW, if you want to get a better feel of how these guys work in binary, you might want to play with std::bitset<8>: this class template has the same bitwise operations, can be constructed from an integer, and when printed shows the individual bits.