#### Category : bit-manipulation

I have been developing software drivers for the MCU peripherals in C++. For part of one register in one of the peripherals it would be convenient for me to have a possibility to work with individual bits along with all bits at once. So I have decided to use union in bit field int main(int ..

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 have a problem for which I have eight elements that can contain 0, 1, or 2. I can easily represent this in 16 bits, but for SIMD efficiency reasons, I need it to occupy 13 bits (it is not the only thing present in the lane). Fortunately, 2^13==8192, and 3^8==6561, so the states I ..

I have got following problem in C++. I have a variable which is a pointer to a mcu peripheral register uint32_t volatile * ctrl_reg; I have defined a function for bit setting in given 32 bits word. This function is intended to be used not only for setting bits in peripheral registers but also in ..

I have some C/C++ code where I have a 16 bit number (uint16_t), and I need to swap the first 5 bits with the last 5 bits, keeping their left-to-right order within each block of 5 bits. The middle 6 bits need to remain intact. I am not great at bit-wise maths/operations so help would ..

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 ..

Question Description : Given an array arr[] of length N, the task is to find the XOR of pairwise sum of every possible unordered pairs of the array. The unordered pairs sum is defined as follows – I solved this question using the method described in this post. My Code : int xorAllSum(int a[], int ..

I have a confused in Left Shift Operator with 1 int a =1; cout<<"size of int: "<<sizeof(int)<<endl; cout <<(a<<31)<<endl; cout <<(a<<32)<<endl; It’s result: size of int: 4 -2147483648 1 It prints 1 if I shift left 32 or 33. How and why it’s 1 and if I want to left shift 1 to be zero, .. 