2

Why do newbies scare from XOR and bitwise operation problems?

 1 year ago
source link: http://codeforces.com/blog/entry/104705
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Why do newbies scare from XOR and bitwise operation problems?

By hunter_of_alts, 7 hours ago,

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 11 it returns 11 otherwise 00, Also it is written as | in C++.

For example :

  • Bitwise OR of 66 and 33 is :

  • 6=1106=110 in binary representation and 3=0113=011.

  • 66 | 3=7=(1103=7=(110 | 011=111)011=111)

  • Note : If in any index a number doesn't have enough bits system supposes that there's a 00 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 11 system return 11 otherwise 00, Also it's written as & in C++.

For example :

  • Bitwise AND of 1010 and 33 is :

  • 10=100110=1001 and 3=00113=0011

  • 1010 & 3=1=10013=1=1001 & 0011=00010011=0001.

3- Bitwise XOR :

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

For example :

  • Bitwise XOR of 33 and 55 and 88 is :

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

  • 88 ^ 55 ^ 3=14=10003=14=1000 ^ 01010101 ^ 0011=11100011=1110.

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK