# Thread: What does this code do? (Bit wise operation)

1. ## What does this code do? (Bit wise operation)

I understand the syntax, actions & sequence of this program, nevertheless, could not answer the question: "describe what does this code do?" Code:
```#include<stdio.h>
main()
{
int i,a,x,num;
unsigned int msk;
a=14;
msk=0x8000;
num=1;
for (i=1;i<17;i++,msk=msk>>1,num*=x)
{
x=1;
if (a&msk)
{
x=i;
}
}
printf ("num=%d\n ",num);
}```
help will be appreciated! 2. Look here if you need help with bits:
http://faq.cprogramming.com/cgi-bin/...&id=1073086407 3. I know this page & I do understand bit wise opration. However I cannot comprehend the job of this code! 4. If you understand all that then you should be able to see what it is doing. 5. If you have 14, in 16bit binary 00000000 00001110
Your code multiplies the indexes of the 1 bits, starting with the more significant. Bits 13, 14, and 15 are set so you output is 13*14*15 = 2730 6. You know, you could have just wrote a piece of code to display the bit pattern of a given variable, and inserted it into the loop to see what was happening. Or you could have just walked through it on paper.

Quzah. 7. I have doen all that & also played with debugger- changed variabels. I have also seen that:
If you have 14, in 16bit binary 00000000 00001110
Your code multiplies the indexes of the 1 bits, starting with the more significant. Bits 13, 14, and 15 are set so you output is 13*14*15 = 2730
...& also more examples, but this does not characterize the thing! I need something more general, not for specific a value. 8. Originally Posted by xErath
If you have 14, in 16bit binary 00000000 00001110
Your code multiplies the indexes of the 1 bits, starting with the more significant. Bits 13, 14, and 15 are set so you output is 13*14*15 = 2730
Ummm, isn't this what you were looking for? You could at least bother to read and understand all the responses. And since you say you understand all the bit operations, it should have been clear. 9. Originally Posted by ronenk
I have doen all that & also played with debugger- changed variabels. I have also seen that:

...& also more examples, but this does not characterize the thing! I need something more general, not for specific a value.

shall we say that the takes a number n and prints the result n(cube)-n 10. >shall we say that the takes a number n and prints the result n(cube)-n
Shall we try again? 11. I have drawed a small tablet with some values off a and num. I still dont know what defined conclusion should I come up with
maybe you can?

a num
1 16
2 15
3 15*16= 240
4 14
5 14*16=224
6 14*15=210
7 14*15*16=3360
8 13
9 13*16
10 13*15=195
11 13*15*16=3120
12 13*14=182
13 13*14*16=2912
14 13*14*15=2730
15 13*14*15*16=43680
16 12
17 12*16=192
18 12*15=180
19 12*15*16=2880
20 12*14 12. ok, it checks each bit in a 16 bit "Value" 'a' against the bit determined in 'msk' one by one if it's set it multiplies the value of 'i'(assigend to x so with this code organization the number wont be incremented before use) with 'num', 'x' being the number of the bit in question.

good enough? i doubt i can explain it more clearly i suck at that. Popular pages Recent additions 