Page 5 of 5 FirstFirst ... 345
Results 41 to 45 of 45

Thread: What's that code snippet?

  1. Top | #41
    Veteran Member
    Join Date
    Dec 2017
    Location
    Land of Smiles
    Posts
    1,068
    Rep Power
    15
    Quote Originally Posted by Jayjay View Post
    Quote Originally Posted by Swammerdami View Post

    A simple one-liner suffices. No loop is needed.
    Finally got it.
    Code:
    int lastbit(int y)
    {
        return ((x-1)^x)&x;
    }
    x-1 sets the last bit to 0, and all preceding bits before it to 1. XOR with original x to clear the irrelevant bits to zero. AND with original leaves only the desired bit active. It seems to work also for 0 and negative numbers, which is neat.
    Beautiful! Variations are possible, e.g. . . . return x&(~(x-1)); . . . The codes work whether the machine uses 2's-complement arithmetic or 1's-complement.

    A programmer showed me this trick 52 years ago and I thought it was wonderful. After all these decades I'm surprised it's not better known, but I guess a large majority of programmers aren't into "bit twiddling" at all.

    Big kudos to Jayjay for discovering the trick himself. We'll never know if I could have figured it out myself: the programmer 52 years ago only gave me a few seconds to think of an answer before revealing it himself.


    (BTW, the trick is not useless! Sometimes you want to loop one-by-one over the 1-bits in a word: you may as well do it quickly.)

  2. Top | #42
    Contributor
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    6,613
    Rep Power
    20
    float p[n] r[n]
    int z = 1

    while(z == 1)(
    z = 0
    for(i = 0, i < n-2, n++){
    If(p(i) > p(i + 1){
    a = p(i);
    p(i) = p(i +1);
    p(i + 1) = a;
    z = 1;
    }
    }

    for(i = 1, i <= n, i++)r(i-1) = i/n);

    plot (x,y) (p,r)

    This s a useful bit of code.

  3. Top | #43
    Veteran Member
    Join Date
    Dec 2017
    Location
    Land of Smiles
    Posts
    1,068
    Rep Power
    15

    Depict visually whether some data follows Zipf's Law?


  4. Top | #44
    Contributor
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    6,613
    Rep Power
    20
    Quote Originally Posted by Swammerdami View Post

    Depict visually whether some data follows Zipf's Law?
    Not directly, it is actually routine analysis. I was not familiar with Zipf's Law but it may be related in some way.

  5. Top | #45
    Contributor
    Join Date
    Nov 2017
    Location
    seattle
    Posts
    6,613
    Rep Power
    20
    Quote Originally Posted by steve_bank View Post
    float p[n] r[n]
    int z = 1

    while(z == 1)(
    z = 0
    for(i = 0, i < n-2, n++){
    If(p(i) > p(i + 1){
    a = p(i);
    p(i) = p(i +1);
    p(i + 1) = a;
    z = 1;
    }
    }

    for(i = 1, i <= n, i++)r(i-1) = i/n);

    plot (x,y) (p,r)

    This s a useful bit of code.
    explanation https://talkfreethought.org/showthre...Practical-Math

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •