#### Category : bitwise-operators

Someone I know was recently asked this question in a coding interview. They were asked to write a function reverseBytes below such that if you pass it 0xabcdef12 it’d output 0x21fedcba. They gave the following solution. int reverseBytes (int x) { int ans = 0; ans = (x & 0xff000000 >> 24) | (x & ..

I’m trying using | inside a IF (or SWITCH) statement to compare if a variable is equal to a number or another. But I found (described as an example in code below) that using | operator to two numbers that I wanna compare is the same result as I put an || for two comparisons. ..

I am trying to understand what is happening in this bitwise operation that is used to set the size of a char array but I do not fully understand it. unsigned long maxParams = 2;// or some other value, I just chose 2 arbitrarily unsigned char m_uParamBitArray[(((maxParams) + ((8)-1)) & ~((8)-1))/8]; What size is this ..

want to apply OR operation between two numbers, but showing the above error , hence am unable to understand what might be the problem! #include <iostream> using namespace std; int main(){ unsigned char a,b; cin>>a>>b; //input two number cout << a | b; //applying bitwise OR operation } Source: Windows Que..

I ran into an issue while overloading friend bitwise operator with template parameters for nested enums. Originally I ran into the issue with proprietary embedded C++ compiler. After that I tried gcc, clang and MSVC and got exactly the same failure. Except, any GCC version prior to 5.0 that I have tried is able to ..

Whenever I am trying to execute int a = 10, b = 20; cout<< a ^ b; The compiler showing an error and at the same time when I am executing int a = 10, b = 20; printf("%d", a ^ b); It was working fine, What could be the problem behind it ? Source: ..

While learning about Fenwick Trees, I found this implementation: Source: https://algorithmist.com/wiki/Fenwick_tree class Fenwick_Tree_Sum { vector<int> tree; Fenwick_Tree_Sum(const vector<int>& Arg)//Arg is our array on which we are going to work { tree.resize(Arg.size()); for(int i = 0 ; i<tree.size(); i++) increase(i, Arg[i]); } // Increases value of i-th element by ”delta”. void increase(int i, int delta) { ..