+ 2
Bitwise Opertaor with example?
explain in details of "&"
2 Answers
+ 5
Here's how to calculate Bitwise "AND":
Example: 84 & 73
Convert numbers to binary;
84 (base 10) = 1010100 (base 2)
73 (base 10) = 1001001 (base 2)
Now we go by what you're probably familiar with; in every column, if both digits are 1, then the result is 1, else 0.
1010100
1001001
---------------
1000000
Thus 84 & 73 = 1000000 (base 2) = 64 (base 10).
+ 2
& also known as 'bitwise and' is used to evaluate logical and at a binary level. So what & does is, convert your number to binary form and evaluate it and then converts it into numeric form.
eg: 7 & 3
00000111 & 00000011 -> binary value of 7, 3
00000011 -> result
3 -> numeric value of result
here you have to be familiar with some of the rules of &
1 & 0 = 0
1 & 1 = 1
0 & 1 = 0
0 & 0 = 0
so evaluation of above example would be:
00000111
& 00000011
= 00000011
converting into numeric form
for that you need to raise 2 to the position numer
so here position numbers are assigned in a reverse order starting from 0
eg
0 0 0 0 0 0 1 1 - binary
7 6 5 4 3 2 1 0 - position number
next step is to raise 2 the position numbers of 1 while 0 will remain as it is
eg
0 0 0 0 0 0 1 1 - binary
7 6 5 4 3 2 1 0 - position number
0 + 0 + 0 + 0 + 0 + 0 + 0 + 2^1 + 2^0 - 2 raised to the position number of all the '1'
= 2^1 + 2^0
= 2 + 1
= 3
i hope this was helpful
if you have any doubt feel free to ask