Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and 1 ( &1) bitwise operation always return 0 or 1

I just started learning about bit wise operation and want to ask why and 1 ( &1) bitwise operation always return 0 or 1 .

like image 949
hung.dev Avatar asked Dec 11 '22 13:12

hung.dev


1 Answers

0 & 0 === 0
0 & 1 === 0
1 & 0 === 0
1 & 1 === 1

therefore any number & 1 will always be either 0 or 1

in binary ... any number

xxxxxxxxxxxxx0

or

xxxxxxxxxxxxx1

where x can be 0 or 1

1 in binary is

00000000000001

so

xxxxxxxxxxxxx1 &
00000000000001 ==
00000000000001

xxxxxxxxxxxxx0 &
00000000000001 ==
00000000000000
like image 115
Jaromanda X Avatar answered Dec 13 '22 03:12

Jaromanda X