0

I am new to programming.

I just want to know, is there any way to shift array elements using the bitwise operator? Maybe with overloading the bitwise operator?

If it is possible, why are we not using this method when bitwise operators are faster compared to arithmetic operators?

Example

int array[] {1, 2, 3};
array << 1; // -> array should become 2, 3, 1
5
  • 1
    Bitwise operators are not faster than arithmetic operators. That might be true in assembly, but it is not true in C++, because C++ compilers will replace arithmetic operations with the equivalent bitwise operations if the bitwise operations are faster. Commented Aug 5, 2020 at 6:52
  • 1
    I'm afraid I don't understand the main question, you should illustrate what you mean with an example. Commented Aug 5, 2020 at 6:54
  • The question is pretty vague. What particular arithmetic operator would you want to replace with a bitwise operator, and what does that have to do with array elements? A code sample would help, as a basis for the question. If your question is simply, can you use a bitwise operator on an array element, then yes. For example, myArray[5] >>= 1 would shift the sixth element of myArray right by one bit. Is that all you're asking? Please clarify. Commented Aug 5, 2020 at 6:55
  • It seems like you have some misconceptions about how C++ operator and operator overloading works. Just because you overload a bitwise operator doesn't mean that the compiler will actually generate bitwise CPU instructions. Commented Aug 5, 2020 at 6:55
  • Also remember that C and C++ are two very different languages. Just the fact that you mention operator overloading means you're in the C++ realm, as C doesn't have it. Please don't tag multiple languages, even if they might seem the same. And please read the help pages, take the SO tour, read How to Ask, as well as this question checklist. Commented Aug 5, 2020 at 6:57

1 Answer 1

4

I just want to know that is there any way to shift array elements using bitwise operator. Maybe with overloading the bitwise operator.

You probably mean to do something like

int array[] {1, 2, 3};
array << 1; // -> array should become 2, 3, 1

This is not possible. For operator overloading one of the operands must be of class type, but neither array, nor 1 are classes. You can overload this operator for containers like std::array or std::vector of course.

template<typename T, size_t N>
void operator<<(std::array<T, N> &arr, int n) {
    // shift things around, the implementation is up to you ;)
}

void foo() {
    std::array<int, 3> array {1, 2, 3};
    array << 1; // -> array becomes 2, 3, 1
}

Even if it is possible why we are not using this method while bitwise operator are the faster compare to arithmetic operators?

Not sure what you mean here. Overloading a bit-shift-operator (or any operator for that matter) doesn't do any actualy bit shifting, it's just a fancy way to call some function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.