What does this code do? (Bit wise operation)

This is a discussion on What does this code do? (Bit wise operation) within the C Programming forums, part of the General Programming Boards category; I understand the syntax, actions & sequence of this program, nevertheless, could not answer the question: "describe what does this ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    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. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Look here if you need help with bits:
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    I know this page & I do understand bit wise opration. However I cannot comprehend the job of this code!

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    If you understand all that then you should be able to see what it is doing.

  5. #5
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    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
    Last edited by xErath; 10-07-2004 at 08:15 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    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. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Quote 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. #9
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote 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. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >shall we say that the takes a number n and prints the result n(cube)-n
    Shall we try again?

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    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. #12
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    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.

    all this was already said in the thread...
    Last edited by no-one; 10-07-2004 at 03:11 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 07:02 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Replies: 1
    Last Post: 01-10-2003, 09:08 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21