zoukankan      html  css  js  c++  java
  • 关于字节对齐公式

    最近在了解BITMAP位图结构,在计算位图行对齐的时候看到了以下2种对齐方式,带来不小困惑

    widthBytes = (width*biBitCount+31)/32*4

    widthBytes = ((width*biBitCount+31)&(~31))>>3

    看上去差别有些大,实际上异途同归, 先看3个对齐公式,可以自己推演

    1. #define ngx_align(d, a)     (((d) + (a - 1)) & ~(a - 1))  //d为未对齐时的数据长度,a为对齐长度,返回的结果为对齐后的长度

    2. #define ngx_align2(d, a)   (((d)+((a)-1))/(a))*(a)

    3.#define ngx_align3(d, a)   (d)%(a)==0 ? (d) : (d)+((a)-(d)%(a));

    1,2,3都可以用作字节对齐,但1有限制条件 a=2^n,  2,3没有限制,但计算效率不及1

    由于width*biBitCount得到的是bit个数, 按照要求是4字节即32bit对齐, 套用公式后,有

    widthBytes = (((width*biBitCount+31)/32)*32)

    或者

    widthBytes = ((width*biBitCount+31)&(~31))

    除以8后,即得到对齐后的字节数

  • 相关阅读:
    SpringSecurity________UsernamePasswordAuthenticationFilter
    Spring Security初步学习
    BASE64
    UUID
    Filter-Chain模式简介
    Servlet容器的简单回顾
    java环境搭建的一些小知识
    Restrictions查询用法
    servlet中session的使用
    strust2中使用session
  • 原文地址:https://www.cnblogs.com/endenvor/p/9916542.html
Copyright © 2011-2022 走看看