bitwise..

Discussion in 'C++' started by cmk128@hotmail.com, Oct 4, 2005.

  1. Guest

    Hi
    C++ should provide us bit shift but fill with 1 rather than 0.
    Or i am wrong, c++ have it???

    if 1 left shift 2, i want 111 rather than 100

    thanks
    from Peter ()
     
    , Oct 4, 2005
    #1
    1. Advertising

  2. Mirek Fidler Guest

    wrote:
    > Hi
    > C++ should provide us bit shift but fill with 1 rather than 0.
    > Or i am wrong, c++ have it???
    >
    > if 1 left shift 2, i want 111 rather than 100


    I am afraid that best you can do is something like (x & 1) | (x << 1).
    AFAIK, most CPUs do not have single instruction for this anyway....

    Mirek
     
    Mirek Fidler, Oct 4, 2005
    #2
    1. Advertising

  3. wrote:
    >
    > Hi
    > C++ should provide us bit shift but fill with 1 rather than 0.


    Why 'should'?

    > Or i am wrong, c++ have it???


    Not everything you or I will ever need os provided by the language. But the
    language is powerful enough that you can synthesize what you need from what
    is existing.

    result = ( value << 1 ) | 0x01;

    does what you want. First shift one position to the left, then
    set the leftmost bit to 1

    For 'shift values' other then 1, I think the easieast approch is
    to use a lookup table:

    unsigned int MyShiftLeft( unsigned int What, unsigned int How )
    {
    static unsigned int OnePattern[] = { 0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F };

    return (What << How ) | OnePattern[How];
    }

    This works for values of 'How' up to 7. If that is not enough, enlarge the table.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 4, 2005
    #3
  4. <> schrieb im Newsbeitrag
    news:...
    > Hi
    > C++ should provide us bit shift but fill with 1 rather than 0.
    > Or i am wrong, c++ have it???
    >
    > if 1 left shift 2, i want 111 rather than 100
    >
    > thanks
    > from Peter ()
    >


    Maybe like this?

    inline unsigned int ASL_FILL(unsigned int num, int sh)
    {
    return (num<<sh) | ((1<<sh)-1);
    }

    int main()
    {
    int test = 0x20;
    test = ASL_FILL(test, 3);
    }
     
    Gernot Frisch, Oct 4, 2005
    #4
  5. <> wrote in message
    news:...

    > C++ should provide us bit shift but fill with 1 rather than 0.
    > Or i am wrong, c++ have it???
    >
    > if 1 left shift 2, i want 111 rather than 100


    To shift x to the left n bits, filling with 1, you can write ~(~x << n)
     
    Andrew Koenig, Oct 4, 2005
    #5
  6. Mirek Fidler Guest

    Mirek Fidler wrote:
    > wrote:
    >
    >> Hi
    >> C++ should provide us bit shift but fill with 1 rather than 0.
    >> Or i am wrong, c++ have it???
    >>
    >> if 1 left shift 2, i want 111 rather than 100

    >
    >
    > I am afraid that best you can do is something like (x & 1) | (x << 1).
    > AFAIK, most CPUs do not have single instruction for this anyway....
    >
    > Mirek


    Oops, sorry, I thought you want to copy LSB... (like arithemtic shift in
    inverse direction).

    Mirek
     
    Mirek Fidler, Oct 4, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dohnut
    Replies:
    0
    Views:
    558
    dohnut
    Oct 20, 2003
  2. dohnut
    Replies:
    1
    Views:
    592
    Sam Holden
    Oct 21, 2003
  3. dohnut
    Replies:
    0
    Views:
    593
    dohnut
    Oct 21, 2003
  4. Random

    bitwise comparator

    Random, Jun 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,166
    Random
    Jun 8, 2005
  5. =?Utf-8?B?Sm9u?=

    BitWise Operations

    =?Utf-8?B?Sm9u?=, Jan 23, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    3,400
    =?Utf-8?B?Sm9u?=
    Jan 24, 2006
Loading...

Share This Page