Thread: Two problems on programming

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    25

    Cool Two problems on programming

    First, I found some coding like this:
    Code:
     
    int current01;
    current01 = !current01
    what does !current01 mean? What will the value of !current01 be?


    Here is another weird code:

    Code:
    #if MKL_PIVOT
    		_rowpivot[k] = _rowpivot[maxrow];
    #else
    		std::swap(_rowpivot[k], _rowpivot[maxrow]);
    #endif
                                    SwapCols(rXmin + halfDeltaX,float(maxrow+.5f));
    Why '#' is added befor if,else, endif? What is the aim doing that?
    (MKL_PIVOT is enum)

    Is that just the same as:

    Code:
    if (MKL_PIVOT)
    		_rowpivot[k] = _rowpivot[maxrow];
    else
    		std::swap(_rowpivot[k], _rowpivot[maxrow]);
    
                                    SwapCols(rXmin + halfDeltaX,float(maxrow+.5f));
    which SwapCols(rXmin + halfDeltaX,float(maxrow+.5f)); will still run anyway?
    Last edited by rosicky2005; 11-06-2006 at 09:39 PM.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    1. current01 will hold an undetermined value since it is not given any value in the first place.
    2. Those are pre-processor conditions. It means that the conditions are evaluated before compilation and that the code matching the condition is "pasted" and the rest is "cut". Therefore, the first #if asks if MKL_PIVOT is a defined macro, if it is, proceed, if it is not, jump to the line after. #endif closes that #if statement and then SwapCols() gets executed no matter what just happened.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    But the code
    Code:
     current01 = !current01
    run for several times. And current01 = 1 initially.

    If !current01 is undetermined value, then is that a swap between 1 and the undetermined will happen?

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    You didn't specify that bit that current01 was set to 1 in the first place. ! is the boolean negation operator. So if the value of current01 is non-zero (or true), it will become false (0) and if it is 0, it will become true (1).

    0 == !1 would evaluate to true because 0 == 0.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Quote Originally Posted by Desolation
    You didn't specify that bit that current01 was set to 1 in the first place. ! is the boolean negation operator. So if the value of current01 is non-zero (or true), it will become false (0) and if it is 0, it will become true (1).

    0 == !1 would evaluate to true because 0 == 0.
    So, it is swap of 1 and 0 if I run several times!!
    Right??

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, it will alternate 0 1 0 1 0 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Salem: Isn't it more like [0] [non-0] [0] [non-0] [0] to be pedantic? Or does the standard specify operator! to return 0/1 only?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    No. Well not for built-in arithmetic types. The operator is called unary-negation. It basically inverts the bits on its operand. Effectively switching between 0 and 1.

    As for non built-in types... the sky is the limit
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    ! is logical negation, ~ is bitwise negation. !0 must be 1.
    Last edited by Daved; 11-07-2006 at 10:05 AM.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Daved
    ! is logical negation, ~ is bitwise negation. !0 must be 1.
    So destructor is the negation of constructor
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM