Find array sum using Bitwise OR after splitting given array in two halves after K circular shift

In C++ splitting an array mean dividing the array into more than one subarray. The Bitwise OR is used to handle the comparison and calculation between two bits or indexes in C++. In this article, we use k circular shift which means the last index position will be shifted to zero index position i.e, the first array element according to k-th times.

Let’s take an example to understand the circular shift into the array.

The given array is 1, 2, 3, 4, 5, 6, 7 and has a length of 6.

Now we will assign the value 3 to k which means k times rotate the circular shift.

The following steps for the operation of a circular shift are −

Step 1 − We are shifting the index[6] to index[1] and then index[5] holds the position of index[6]. The first circular shift becomes 7,1,2,3,4,5,6 and this way circular shift will rotate.

Step 2 − Second circular shift- 6,7,1,2,3,4,5

Step 3 − Third circular shift- 5,6,7,1,2,3,4(Final Outcome)


vector <data_type>l; vector_name(parameter 1, parameter 2)
  • Vector is used as a keyword and the datatype is a type of data mentioned by the user. In the end, vector_name represents the name of the vector assigned by user.

  • The first parameter specifies the size of the vector. The second parameter is the value that is used to initialize each element of the vector.

splitArray[i%2] = splitArray[i%2] | arr[(i+k)%arr.size()]
  • The mod(%) operator split the array into two halves and by using bitwise OR ‘|’ we are getting the value of each element in the two halves.

  • The ‘arr[(i+k)%arr.size()]’ represents a shifting index while performing bitwise OR it will give one of the two elements in a split array and this is based on the correspondence of ‘splitArray[i%2]’.


  • We will start the program with header files namely ‘iostream’ and ‘vector’.

  • We will define a function called ‘Split_arr_sum_bitwise’ that takes ‘arr’ and ‘k’ as a parameter. This function receives the array value and the updated array value after the circular shift.

  • We will initialize the vector variable named ‘splitArray’ inside the ‘Split_arr_sum_bitwise’ function. This function will store the two halves of the array.

  • Next, store the value 0 in the variable sum that will meet later for the addition using bitwise OR ‘|’ with the array of a function named ‘splitArray’.

  • Then we create the first for loop, here we will iterate the original array.

  • Then we will create a second for loop that will calculate the sum of two halves using Bitwise OR ‘|’. This operator will find the split array sum into two halves after K circular shift.

  • Now start the main function where we initialize the array value to ‘array’ variable and store the value ‘3’ in the variable ‘k’ that will define the number of circular shift of the given array.

  • Finally, in the printing statement we are calling the function named ‘Split_arr_sum_bitwise’ and passing the parameter ‘K’and ‘array’ to it for the final output.


In this program, we are going to implement the array sum using bitwise OR and then split the array into two halves after K circular shift.

#include <iostream>
#include <vector>
using namespace std;
int Split_arr_sum_bitwise(vector<int>& arr, int k) {
   vector<int> splitArray(2,0);
   int sum = 0;
   // Splitting the array into two halves after K-Circular Shift.
   for (int i = 0; i < arr.size(); i++) {
      splitArray[i % 2] = splitArray[i % 2] | arr[(i + k) % arr.size()];
   // Sum of two halves using bitwise OR
   for (int i = 0; i < splitArray.size(); i++) {
      sum = sum | splitArray[i];
   return sum;
int main() {
   vector<int> array = {1, 2, 3, 4, 5, 6, 7};
   int k = 3;
   cout <<"The split sum of array halves using bitwise OR is "<<Split_arr_sum_bitwise(array, k) << endl;
   return 0;


The split sum of array halves using bitwise OR is 7


We explored the concept of K circular shift where an array is split into two halves and also understand how bitwise OR performs the sum to store the value of a split array. The % mod operator split the array into two halves which shows the calculation of odd and even to the index position of an array.

Updated on: 10-May-2023


Kickstart Your Career

Get certified by completing the course

Get Started