Memory alignment # DEFINE _ intsizeof (N) (sizeof (n) + sizeof (INT)-1 )&~ (Sizeof (INT)-1 ))

Source: Internet
Author: User
For two positive integers x and N, the total number of integers Q, R makes

X = NQ + R, where 0 <= r <N // minimum non-negative residue

Q and R are uniquely identified. Q = [x/n], r = x-N [x/n]. This is a simple form with remainder division. In C, Q and R are easy to calculate: q = x/N, r = x % N.


Alignment of X by N refers to: If r = 0, take Qn. If R> 0, take (q + 1) n. This is equivalent to representing X as follows:

X = NQ + R', where-n <R' <= 0 // The maximum is not positive.

NQ is what we want. The key is how to calculate it in C language. Since we can process the standard Division with remainder, we can convert this formula into a standard Division with remainder, and then process it:

X + n = qn + (n + R'), where 0 <n + R' <= N // The largest positive Remainder

X + n-1 = qn + (n + R'-1), where 0 <= N + R'-1 <N // minimum non-negative residue

Therefore, qN = [(x + N-1)/n] n. Calculated in C language:

(X + N-1)/n) * n

If n is the power of 2 For example, 2 ^ m is used to remove the right M bit, and the multiplication is used to shift the left M bit. So we can clear the minimum M binary bits of X + n-1 to 0. Get:

(X + N-1 )&(~ (N-1 ))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.