Xor all elements in list python

In Python 3 you can use:

>>> from functools import reduce
>>> from operator import xor
>>> bits = ['0', '1', '0', '1', '0', '1', '0']
>>> reduce[xor, map[int, bits]]
1

Or if you want a running XOR:

>>> from itertools import accumulate
>>> from operator import xor
>>> bits = ['0', '1', '0', '1', '0', '1', '0']
>>> list[accumulate[map[int, bits], xor]]
[0, 1, 1, 0, 0, 1, 1]

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among list elements. This is an essential utility as we come across bitwise operations many times. Let’s discuss certain ways in which XOR can be performed.

    Method #1 : Using reduce[] + lambda + “^” operator
    The above functions can be combined to perform this task. We can employ reduce[] to accumulate the result of XOR logic specified by the lambda function. Works only with Python2.

    test_list = [4, 6, 2, 3, 8, 9]

    print["The original list is : " + str[test_list]]

    res = reduce[lambda x, y: x ^ y, test_list]

    print["The Bitwise XOR of list elements are : " + str[res]]

    Output :

    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    

    Method #2 : Using reduce[] + operator.ixor
    This task can also be performed using this method. In this the task performed by lambda function in above method is performed using ior function for cumulative XOR operation. Works with Python2 only.

    from operator import ixor

    test_list = [4, 6, 2, 3, 8, 9]

    print["The original list is : " + str[test_list]]

    res = reduce[ixor, test_list]

    print["The Bitwise XOR of list elements are : " + str[res]]

    Output :

    The original list is : [4, 6, 2, 3, 8, 9]
    The Bitwise XOR of list elements are : 2
    


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given an array arr[] containing integers of size N, the task is to find the XOR of this array.
    Examples: 
     

    Input: arr[] = {2, 4, 7} 
    Output:
    Explanation: 
    XOR of the array = 2 ^ 4 ^ 7 = 1
    Input: arr[] = { 3, 9, 12, 13, 15 } 
    Output:
     

    Approach: In order to find the XOR of all elements in the array, we simply iterate through the array and find the XOR using ‘^’ operator. Therefore, the following steps are followed to compute the answer: 
     

    1. Create a variable to store the XOR of the array as a result.
    2. For each element in the array, find the XOR of the element and the result variable using ‘^’ operator.
    3. Finally, the result variable stores the XOR of all elements in the array.

    Below is the implementation of the above approach: 
     

    CPP

    #include

    using namespace std;

    int xorOfArray[int arr[], int n]

    {

        int xor_arr = 0;

        for [int i = 0; i < n; i++] {

            xor_arr = xor_arr ^ arr[i];

        }

        return xor_arr;

    }

    int main[]

    {

        int arr[] = { 3, 9, 12, 13, 15 };

        int n = sizeof[arr] / sizeof[arr[0]];

        cout

    Chủ Đề