zero the last 13 bits of an unsigned long number

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Jun 27, 2006.

  1. Guest

    Hi guys,

    I have some value stored in an unsigned long integer. My
    requirement is to
    zero the 13 LSB bits of this number. Or more specifically, I want to
    make the number
    8k (8192) aligned. For instance, if the value is 26 kilobytes I should
    get 24kilobytes.
    Can somebody tell me a portable way of doing it. I thought of
    using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
    some other
    machine. I also thought of right shifting the number by 13 bits and
    again left shifting it
    by 13. But, I don't know if this is a good way of soing it.
    Can anyone sugggest me a cleaner way of doing it ?

    thanks a lot for any help ...
     
    , Jun 27, 2006
    #1
    1. Advertising

  2. Skarmander Guest

    wrote:
    > Hi guys,
    >
    > I have some value stored in an unsigned long integer. My
    > requirement is to
    > zero the 13 LSB bits of this number. Or more specifically, I want to
    > make the number
    > 8k (8192) aligned. For instance, if the value is 26 kilobytes I should
    > get 24kilobytes.
    > Can somebody tell me a portable way of doing it. I thought of
    > using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
    > some other
    > machine. I also thought of right shifting the number by 13 bits and
    > again left shifting it
    > by 13. But, I don't know if this is a good way of soing it.
    > Can anyone sugggest me a cleaner way of doing it ?
    >


    #include <limits.h>
    unsigned long mask_lsb13(unsigned long x) {
    return x & (ULONG_MAX << 13);
    }

    S.
     
    Skarmander, Jun 27, 2006
    #2
    1. Advertising

  3. Tom St Denis Guest

    wrote:
    > Hi guys,
    >
    > I have some value stored in an unsigned long integer. My
    > requirement is to
    > zero the 13 LSB bits of this number. Or more specifically, I want to
    > make the number
    > 8k (8192) aligned. For instance, if the value is 26 kilobytes I should
    > get 24kilobytes.
    > Can somebody tell me a portable way of doing it. I thought of
    > using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
    > some other
    > machine. I also thought of right shifting the number by 13 bits and
    > again left shifting it
    > by 13. But, I don't know if this is a good way of soing it.
    > Can anyone sugggest me a cleaner way of doing it ?


    Um what about

    x &= ~((1UL<<13) - 1UL)

    btw the mask should be 0xFFFFE000 for 32-bit platforms :)

    Tom
     
    Tom St Denis, Jun 27, 2006
    #3
  4. <> wrote:
    > I have some value stored in an unsigned long integer. My
    >requirement is to
    >zero the 13 LSB bits of this number. Or more specifically, I want to
    >make the number
    >8k (8192) aligned. For instance, if the value is 26 kilobytes I should
    >get 24kilobytes.
    >Can somebody tell me a portable way of doing it. I thought of
    >using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
    >some other
    >machine. I also thought of right shifting the number by 13 bits and
    >again left shifting it
    >by 13. But, I don't know if this is a good way of soing it.
    >Can anyone sugggest me a cleaner way of doing it ?


    unsigned long mask = ~0x1fffUL;
     
    Roberto Waltman, Jun 27, 2006
    #4
  5. On Tue, 27 Jun 2006 07:35:46 -0700, wrote:

    > Hi guys,
    >
    > I have some value stored in an unsigned long integer. My
    > requirement is to
    > zero the 13 LSB bits of this number. Or more specifically, I want to
    > make the number
    > 8k (8192) aligned. For instance, if the value is 26 kilobytes I should
    > get 24kilobytes.
    > Can somebody tell me a portable way of doing it. I thought of
    > using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
    > some other
    > machine. I also thought of right shifting the number by 13 bits and
    > again left shifting it
    > by 13. But, I don't know if this is a good way of soing it.
    > Can anyone sugggest me a cleaner way of doing it ?
    >
    > thanks a lot for any help ...

    It's conceivable, I suppose, that you might save a femtogrunt by twiddling
    bits, but I think arithmetic is clearer:
    size -= size % 8192;
    (and a good optimiser may well implement that as bit twiddling)
    Duncan
     
    Duncan Muirhead, Jun 27, 2006
    #5
  6. Guest

    Duncan Muirhead wrote:
    > On Tue, 27 Jun 2006 07:35:46 -0700, wrote:
    >
    > > Hi guys,
    > >
    > > I have some value stored in an unsigned long integer. My
    > > requirement is to
    > > zero the 13 LSB bits of this number. Or more specifically, I want to
    > > make the number
    > > 8k (8192) aligned. For instance, if the value is 26 kilobytes I should
    > > get 24kilobytes.
    > > Can somebody tell me a portable way of doing it. I thought of
    > > using a mask 0xfffe0000, but that won't work if long is 8 bytes long on
    > > some other
    > > machine. I also thought of right shifting the number by 13 bits and
    > > again left shifting it
    > > by 13. But, I don't know if this is a good way of soing it.
    > > Can anyone sugggest me a cleaner way of doing it ?
    > >
    > > thanks a lot for any help ...

    > It's conceivable, I suppose, that you might save a femtogrunt by twiddling
    > bits, but I think arithmetic is clearer:
    > size -= size % 8192;
    > (and a good optimiser may well implement that as bit twiddling)


    thanks Dunkan. This really looks great ...
     
    , Jun 27, 2006
    #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. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    752
    Eric Sosman
    Jul 8, 2003
  2. sarmin kho
    Replies:
    2
    Views:
    855
    A. Lloyd Flanagan
    Jun 15, 2004
  3. Miki Tebeka
    Replies:
    1
    Views:
    465
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
  4. Digital Puer

    extracting front bits from an unsigned long long?

    Digital Puer, Nov 11, 2005, in forum: C Programming
    Replies:
    36
    Views:
    1,384
    Mark McIntyre
    Nov 13, 2005
  5. krunalb
    Replies:
    10
    Views:
    933
    Kenneth Brody
    Jan 23, 2007
Loading...

Share This Page