Anyone have an idea how see if a 32 bit integer has an even or odd number of 1 bits using ONLY bitwise operations?
First, ! and + aren't bitwise operators. Second, yes, I know several ways of doing this. Third, since this is invariably a homework question, the task is for you to do it, not for you to ask someone else for the solution.
Though I'll be nice and provide a partial solution since counting bits without a loop is somewhat non-trivial. It's partial because arithmetic operators are used as well. Your job now is to replace them with bitwise operators:
unsigned bitcount(unsigned v)
unsigned c = 0;
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
Alternatively, you could be pedantic and use the implicit test against 0 for a loop:
unsigned value = n;
while (value) /* Equivalent to "while (value != 0)" */
value >>= 1;
Some teachers may not allow that though, so I wouldn't recommend it without checking first.
Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...
I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...