Thread: is tha good method?

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Heh
    Code:
        a = a + b;
        b = a - b;
        a = a - b;
    produces
    	movl	-8(%ebp), %edx
    	leal	-4(%ebp), %eax
    	addl	%edx, (%eax)
    	movl	-8(%ebp), %edx
    	movl	-4(%ebp), %eax
    	subl	%edx, %eax
    	movl	%eax, -8(%ebp)
    	movl	-8(%ebp), %edx
    	leal	-4(%ebp), %eax
    	subl	%edx, (%eax)
    vs.
    Code:
            int temp = a;
            a = b;
            b = temp;
    produces
    	movl	-4(%ebp), %eax
    	movl	%eax, -12(%ebp)
    	movl	-8(%ebp), %eax
    	movl	%eax, -4(%ebp)
    	movl	-12(%ebp), %eax
    	movl	%eax, -8(%ebp)
    10 instructions vs. 6
    and no undefined behaviour....


    > Temp variable swap takes more time because of the temp variable creation
    Creating 2 or 20 local variables takes the same amount of time, and since it's always done if you have any local variables at all (and takes only one instruction anyway), I fail to see your point.
    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.

  2. #17
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I thought that integer overflow was undefined behavior in ANSI?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #18
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Unsigned integer under/overflow is defined behavior (it wraps around)
    signed interger under/overflow is undefined behavior

  4. #19
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    its called C

    use it at your own risk... in other words dont use it unless you know what the hell your doing... this is whats wrong with modern languages... they assume your an idiot. This is why C will never die.

    Applies to everything... give up freedom/power for security/safty.... generally a bad idea most of the time...

    on the subject, is it a good method, depends on what your doing, but generally you should read what Salem said, let the optimizer do its job... cause it will do it better than you.
    Last edited by no-one; 07-25-2004 at 01:49 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #20
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    one "cute trick" that a book said was this
    Code:
    if(!var&1) /*number is even*/
    is that always defined? I know endianness is an issue for the MSB depending on which side it is on, so I don't know about this.

  6. #21
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'm not really sure on the order of precedence in that situation but there really is only two possibilities
    !(var&1) and (!var)&1

    So given var == 4 using eight bits:
    Code:
    var  == 00000100
    1    == 00000001
    !var == 11111011
    
    (var & 1)  == 00000000
    !(var & 1) == 11111111
    
    (!var & 1) == 00000001
    Well both come up with a true statement but lets check on a odd number (var == 3)
    Code:
    var  == 00000011
    1    == 00000001
    !var == 11111100
    
    (var & 1)  == 00000001
    !(var & 1) == 11111110
    
    (!var & 1) == 00000000
    Well the last one is the only one that came up with a false statement. Though as always I'd avoid bit-wise tricks whenever possible

  7. #22
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    well actually I just shortened it myself. ! is evaluated with & from left to right. The code in the book was
    Code:
    if((var&1)==0){

  8. #23
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Heh I try not to memorize certain things, there is a tendacy to remember wrong at the worst possible times

  9. #24
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    don't worry I looked it up in C programming language[page 53 ]

  10. #25
    Quote Originally Posted by linuxdude
    one "cute trick" that a book said was this
    Code:
    if(!var&1) /*number is even*/
    is that always defined? I know endianness is an issue for the MSB depending on which side it is on, so I don't know about this.
    As long as you don't access to the data byte by byte, there is no endianness issues. Bitwise operators are portable with unsigned integers (char, short, int, long...)
    Last edited by Emmanuel Delaha; 07-25-2004 at 03:00 PM. Reason: missing comma
    Emmanuel Delahaye

    "C is a sharp tool"

  11. #26
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I know endianness is an issue for the MSB depending on which side it is on
    Doesn't matter what endian the machine is - big, little, middle or whatever
    var & 1 will always test the least significant bit
    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.

  12. #27
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Thantos
    I'm not really sure on the order of precedence in that situation but there really is only two possibilities
    !(var&1) and (!var)&1

    So given var == 4 using eight bits:
    Code:
    var  == 00000100
    1    == 00000001
    !var == 11111011
    
    (var & 1)  == 00000000
    !(var & 1) == 11111111
    
    (!var & 1) == 00000001
    Well both come up with a true statement but lets check on a odd number (var == 3)
    Code:
    var  == 00000011
    1    == 00000001
    !var == 11111100
    
    (var & 1)  == 00000001
    !(var & 1) == 11111110
    
    (!var & 1) == 00000000
    Well the last one is the only one that came up with a false statement. Though as always I'd avoid bit-wise tricks whenever possible
    You've got a slight problem there... ! anything which is non-zero is zero. ! anything zero is 1.
    Code:
    #include <stdio.h>
    #include <limits.h>
    void showbits( unsigned int x )
    {
            int y;
            for( y = 0; y < sizeof x * CHAR_BIT; y++ )
                    putchar( (x & (1<<y)) ? '1' : '0' );
    }
    
    
    int main ( void )
    {
            unsigned int x = !4;
            int z;
    
            printf("x is %u\n", x );
            printf("Bits: ");
            showbits( x );
            printf("\n");
    
            return 0;
    }
    I don't know what you were meaning to show with your bit pattern, but it's wrong in the example where you're showing "!var". "!nonzero" is always zero. Which is why we can do instead:
    Code:
    void showbits( unsigned int x )
    {
            int y;
            for( y = 0; y < sizeof x * CHAR_BIT; y++ )
                    printf("%d", !!(x&(1<<y)) );
    }
    Unless I've misunderstood how you were trying to illustrate that.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #28
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No your right quzah, for some reason I mixxed up ! and ~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 05:17 PM
  2. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  3. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Need a good method
    By Asagohan in forum Tech Board
    Replies: 11
    Last Post: 04-08-2005, 04:57 PM