Guess the code!

This is a discussion on Guess the code! within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Laserve what if you'd do (value & bit) != 0 ? isnt that either 0 or 1 ...

  1. #91
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Laserve
    what if you'd do (value & bit) != 0 ? isnt that either 0 or 1 ?
    It would be the same.
    Quote Originally Posted by jafet
    But!! isn't!! double!! exclaimation!! so!! leet!! ??!!
    No, it's a C idiom that I figured was not widely recognized, but I didn't know how much.

    I first saw it about a decade or so ago, and I've come to read it as a "move bit to LSB" "operator". Mentally I picture it something like this:
    Code:
    0100:0000 -> 0000:0001, 0000:0000 -> 0000:0000
    So upon inspection, I can more easily tell that this is doing what is expected like this:
    Code:
    #define UART_RX 0x40
    const volatile unsigned char *UartStatus = (const volatile unsigned char *)0x38;
    volatile unsigned char UartRxData;
    
    /* interrupt */ void UartReceive(void)
    {
       UartRxData <<= 1; /* shift current value to make room for new LSB */
       UartRxData  |= !!(*UartStatus & UART_RX); /* add new bit in LSB */
    }
    than like this
    Code:
    #define UART_RX 0x40
    const volatile unsigned char *UartStatus = (const volatile unsigned char *)0x38;
    volatile unsigned char UartRxData;
    
    /* interrupt */ void UartReceive(void)
    {
       UartRxData <<= 1; /* shift current value to make room for new LSB */
       UartRxData  |= (*UartStatus & UART_RX) != 0; /* add new bit in LSB */
    }
    or other variants. YMMV.

    Related discussions:

    Quote Originally Posted by manutd
    Ah! Not the leet thing again!




    Apparently the point of this thread is not what I thought.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #92
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by Dave_Sinkula
    It would be the same.No, it's a C idiom that I figured was not widely recognized, but I didn't know how much.

    I first saw it about a decade or so ago, and I've come to read it as a "move bit to LSB" "operator".
    Dang... I know I've had situations where I've needed something like that. So perhaps not widely recognized, but really pretty simple in how it works. Neat trick.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #93
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    > what if you'd do (value & bit) != 0 ? isnt that either 0 or 1 ?

    It's the same as (value & bit) && true as well.

  4. #94
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Based on the same principle:
    Code:
    int foo(int n) {
        int a = n, b = 1;
        while(a-b != !!(a - b)) a = a/2+b/2+(!!a+!!b)/2, b = n/a;
        return b;
    }
    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;}

Page 7 of 7 FirstFirst 1234567
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. banking2
    By sweet2awy in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2003, 04:01 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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