hunter_of_alts's blog

By hunter_of_alts, 2 years ago, In English

Hello, The main reason of newbies' fear from XOR is bad reference in the problem statements to explain XOR and other bitwise operations (Authors usually put Wikipedia reference) and newbies mostly can't understand what's XOR, today I wanna explain something about bitwise operations for this group of coders with simple language :

1- Bitwise OR :

In this operation the system compares bits in same index in binary representation and if at least one of the bits was $$$1$$$ it returns $$$1$$$ otherwise $$$0$$$, Also it is written as | in C++.

For example :

  • Bitwise OR of $$$6$$$ and $$$3$$$ is :

  • $$$6 = 110$$$ in binary representation and $$$3 = 011$$$.

  • $$$6$$$ | $$$3 = 7 = (110$$$ | $$$011 = 111)$$$

  • Note : If in any index a number doesn't have enough bits system supposes that there's a $$$0$$$ bit in that index in binary representation of that number.

2- Bitwise AND :

As it's guessable from it's name if all bits in same index were $$$1$$$ system return $$$1$$$ otherwise $$$0$$$, Also it's written as & in C++.

For example :

  • Bitwise AND of $$$10$$$ and $$$3$$$ is :

  • $$$10 = 1001$$$ and $$$3 = 0011$$$

  • $$$10$$$ & $$$3 = 1 = 1001$$$ & $$$0011 = 0001$$$.

3- Bitwise XOR :

In this operation system compares bits in same index and if odd number of them were $$$1$$$ it returns $$$1$$$ otherwise $$$0$$$, Also it is written as xor or ^ in C++.

For example :

  • Bitwise XOR of $$$3$$$ and $$$5$$$ and $$$8$$$ is :

  • $$$8 = 1000, 5 = 0101, 3 = 0011$$$.

  • $$$8$$$ ^ $$$5$$$ ^ $$$3 = 14 = 1000$$$ ^ $$$0101$$$ ^ $$$0011 = 1110$$$.

I hope it would be helpful for you 💗 💗 (BTW if you noticed any typo let me know).

PS : As people say in the comments I've added some educational problems about this topic, If you had problems about this topic please write down in the comments Thanks.

  • Vote: I like it
  • +14
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
Rev. 2   Vote: I like it +12 Vote: I do not like it

There are only 10 types of people...

  • »
    »
    2 years ago, # ^ |
      Vote: I like it +40 Vote: I do not like it

    1.those who know ternary system
    2.those who doesn't know ternary system
    3.those who thought this is a joke about binary system

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it +6 Vote: I do not like it
      1. those who know quaternary system
      2. those who doesn't know quaternary system
      3. those who thought this is a joke about binary system
      4. people like you, who thought this is a joke about ternary system