I have been given a question to convert base from 10 to 2 without using division(/) and modulus(%), 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 points 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 values except 0 and 1 and still have an 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 remainder (according which mathematical operations and logic)?
The
>>
operator in C++ always does binary shifting, never decimal shifting. There is no decimal shifting operator. You're welcome to write your own function that does that, if you want one.Although it's not wrong to think of mathematical division by 10 as a shift by one decimal place, that's not how C++ does shifting. Also, it's shifting to the right, not the left — look which way the brackets are pointing.
You've also misunderstood bitwise definitions. It's true that Y & 1 = Y, when Y is a bit. When Y is more than just one bit, the definition is extended to apply the one-bit definition to each bit in the two operands. That's what bitwise means. The operator is applied bitwise to the operands: The first bit of the left operand is combined with the first bit of the right operand to yield the first bit of the result. Likewise, the second bits of each of the two operands determine the second bit of the result, and so on for each pair of bits in the operands.
To calculate the remainder from dividing two numbers, use the
%
operator, also known as the modulo operator. Read more about it in your C++ textbook.