Determine if a number lies on a 4-byte boundary

This is a discussion on Determine if a number lies on a 4-byte boundary within the C Programming forums, part of the General Programming Boards category; I've been racking my mathless brain for the last 45 minutes, looked at two books (if Bit Twiddling Hacks counts ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Determine if a number lies on a 4-byte boundary

    I've been racking my mathless brain for the last 45 minutes, looked at two books (if Bit Twiddling Hacks counts as a book) all to no avail.

    The answer is no doubt really obvious. I'm tired. Very, very tired.

    EDIT: Yes, I have googled.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I'm not quite sure what you're asking here. If you want to find whether a number can be stored in 4 bytes, I'd check whether it's less than 2^32.

    QuantumPete
    Last edited by QuantumPete; 09-07-2007 at 12:23 PM. Reason: made stupid mistake
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I want to know if it's dead on a 4-byte boundary, like is it equal to 32, 64, 96 etc. I guess I could use a lookup table with a search, but I was hoping there's a way of calculating it.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Hmm, you could mod it with for 4...
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Or I could shoot myself in the FACE for being so stupid. Thank you.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by ahluka View Post
    Or I could shoot myself in the FACE for being so stupid. Thank you.
    No problem ;-)
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I want to know if it's dead on a 4-byte boundary, like is it equal to 32, 64, 96 etc.
    Code:
        if ((num & 0x1f) == 0)

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The normal way to check the alignment is to and with alignment-1 and check that it's zero, e.g.
    Code:
       if ((address & 3) == 0) ... // it's aligned
       else ... // unaligned.
    If you want to make it generic, you could do:
    Code:
       if ((address & sizeof(type)-1) == 0) ...
    This works for all types that have sizes that are 2^n, e.g 2, 4, 8, 16, 32, 64, etc.

    Note that the parenthesis around (address & x) are necessary, otherwise the compiler will evaluate 3 == 0 [or whatever] first, then perform the and. If it's false, like in this case, it will be false all over, since X & 0 is always zero, so false. Don't ask me how I remember this, just trust me that if you've made the same mistake enough times, you don't forget quite so easily.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 09:43 PM
  3. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM
  4. Perfect number
    By TheSki in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 04:34 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-26-2001, 12:45 AM

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