Thread: [C] Setbits function

  1. #16
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Sebastiani View Post
    Sorry, guys, I ran both of your functions and each returned the wrong results. For instance, setbits(85, 170, 1, 4) should output 75...
    ...
    Or am I missing something here?
    IMO your interpretation of the order of the arguments to setbits() is misplaced. The arguments to setbits(x, p, n, y) are: x is a number whose "n" bits located at "p" need to match the rightmost "n" bits of y.

    Edit: based on the above, it should be called as setbits(85, 4, 1, 170) where
    the bit (of x=85) located at bit position 4 needs to match the LSB of the number y (=170)
    called such, setbits() outputs 69
    Last edited by itCbitC; 07-08-2009 at 09:44 PM.

  2. #17
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> IMO your interpretation of the order of the arguments to setbits() is misplaced. The arguments to setbits(x, p, n, y) are: x is a number whose "n" bits located at "p" need to match the rightmost "n" bits of y.

    Ok, you're right, I misread the requirements there. But there does seem to be a bug somewhere in your code. I ran it again on the values given by the OP and only dwks' gave the correct result.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #18
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Sebastiani View Post
    Ok, you're right, I misread the requirements there. But there does seem to be a bug somewhere in your code...
    IBTD, but perhaps you can point to where the problem in the code maybe.

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> IBTD, but perhaps you can point to where the problem in the code maybe.

    Are you kidding? I don't even understand the problem clearly, to be quite honest! I mean, if you look at the OP's first post it sets the the value of 'a' to 0x01234567, or in (32 bit) binary:

    Code:
    0000 0001 0010 0011 0100 0101 0110 0111
    Now if 'p' is 30, then we want a mask for 'a' of:

    Code:
    0011 1111 1111 1111 1111 1111 1111 1111
    So at the very least, the result is going to be:

    Code:
    XX00 0001 0010 0011 0100 0101 0110 0111
    Where X is a position to be filled by bits from 'b'. How then did they arrive at:

    Code:
    0101 0011 0000 0011 0100 0101 0110 0111
    I just don't get it!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #20
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Sebastiani View Post
    Are you kidding? I don't even understand the problem clearly, to be quite honest! I mean, if you look at the OP's first post it sets the the value of 'a' to 0x01234567, or in (32 bit) binary...
    All right then call the function as setbits(290, 7, 2, 87).

    x = 290 (100100010)
    y = 87 (1010111)

    Now the 2 bits of x at position 7 must match the 2 rightmost bits of y.
    After setbits() returns the new value of x will be 482 (111100010).

  6. #21
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Now the 2 bits of x at position 7 must match the 2 rightmost bits of y.

    I see. The notation is a bit confusing then. I would consider bit position 7 to be the 8th bit (zero-based index) and the rightmost bits of a number to be the most significant.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not sure what you intended by the number in red below.
    Just that. It's -1u. -1 converted to type unsigned. It's a quick and dirty way to get all 1's in a number. Not very portable. I suppose using ~0 would be better.

    I ran it again on the values given by the OP and only dwks' gave the correct result.
    Hooray! Anyway, I'm not going to take another stab at it unless the OP defines the original problem a little more clearly . . . .

    EDIT: Nice syntax highlighting, BTW. Uh, do you mind if I borrow it?
    Do borrow it! That's what it's there for. You can run it online or download the source from dwks.theprogrammingsite.com/myprogs/codeform.htm (it's GPL'd).

    If you're running on Windows, I wrote a bunch of other nifty features into codeform. Like, you can make it so that when you copy code starting with "//cf", codeform codeforms the code in the background, and when you paste the code, it's automatically syntax-highlighted. Another variant makes it so that if you shift-ctrl-c, codeform again processes the contents of the clipboard and replaces it with the codeformed code.

    There was a thread about codeform a while back; look it up (perhaps it's in my signature . . .) if you're interested.

    It would make me happy if you "borrowed" it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #23
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Sebastiani View Post
    I see. The notation is a bit confusing then. I would consider bit position 7 to be the 8th bit (zero-based index)...
    That's correct since bit positions start at zero, so the 8th bit is at bit position 7.
    Quote Originally Posted by Sebastiani View Post
    ...and the rightmost bits of a number to be the most significant.
    Nope! endianess isn't the same as bit significance.

    In big endian architecture, the most significant byte is stored in the lowest memory location.
    In little endian scheme the most significant byte is stored in the highest memory location, as in
    Code:
    int x = 0xa1b2c3d4;  /* big endian:  MSB a1 is in memory cell numbered 0 while LSB d4 is in 3 */
    int x = 0xa1b2c3d4;  /* little endian:  MSB a1 is in memory cell numbered 3 while LSB d4 is in 0 */

  9. #24
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> There was a thread about codeform a while back; look it up (perhaps it's in my signature . . .) if you're interested.

    Ok, thanks.

    >> Nope! endianess isn't the same as bit significance.

    I wasn't even thinking in terms of endianness, actually. I think I'm just going to plead insanity on this one.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #25
    Registered User
    Join Date
    Jul 2014
    Posts
    1

    setbits

    Code:
    int setbits(unsigned , unsigned , unsigned , unsigned)
    {
         return (( x| ~( ~0 << p+1 | ~(~0 << p-n+1))) & (((y & ~(~0 << n)) << p-n+1 ) | (~(~0 << p-n+1)) | (~0 << p+1)));
    }

  11. #26
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    How is it that a new user is able to bump a five year old thread? Shouldn't they all be auto-locked? (I recall this happening a few months ago, as well.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM