在音频流解析过程中,常常会涉及到顺序读取某些bit的操作。

#include<stdio.h>

typedef struct _BIT_BUF {
unsigned char buffer[1024];
unsigned int byteSize;
unsigned int bytePosition;
unsigned int bitPosition;
unsigned int wBytePosition;
unsigned int wBitPosition;
}tBitBuffer;

//mask:baoliu保留低bit位,可用while 循环0x1右移bit-1 位。

unsigned int mask(unsigned int bit){
  switch (bit) {
    case 1:
      return 0x1;
    break;
    case 2:
      return 0x3;
    break;
    case 3:
      return 0x7;
    break;
    case 4:
      return 0xf;
    break;
    case 5:
      return 0x1f;
    break;
    case 6:
      return 0x3f;
    break;
    case 7:
      return 0x7f;
    break;
    case 8:
      return 0xff;
    break;

  case 9:
    return 0x1ff;
  break;
  case 10:
    return 0x3ff;
  break;
  case 11:
    return 0x7ff;
  break;
  case 12:
    return 0xfff;
  break;
  case 13:
    return 0x1fff;
  break;
  case 14:
    return 0x3fff;
  break;
  case 15:
    return 0x7fff;
  break;
  case 16:
    return 0xffff;
  break;

  }
}

//顺序读取bitbuffer 中的bits位,返回结果
unsigned long bitBuffer_readBits(tBitBuffer *pBitBuffer, unsigned int bits) {
  unsigned long result = 0;
  if (bits > 64) {
    return 0;
  }
  

  if (pBitBuffer->bytePosition * 8 + (pBitBuffer->bitPosition + bits ) >pBitBuffer->wBytePosition * 8 + pBitBuffer->wBitPosition) {
  printf("L%d, error,no data,bytePos:%d, bitPos:%d, wBytePos: %d, wBitPos:%d\n",__LINE__, pBitBuffer->bytePosition, pBitBuffer->bitPosition, pBitBuffer->wBytePosition,     pBitBuffer->wBitPosition);
  return 0;
}

  unsigned int bitsRemainning = 8 - pBitBuffer->bitPosition;
  while (bits != 0) {
    printf("bits:%d,bitPositon:%d, bitsRemainning:%d\n", bits, pBitBuffer->bitPosition, bitsRemainning);
    if (bitsRemainning > bits) {
      result <<= bits;
      pBitBuffer->bitPosition += bits;
      bitsRemainning -= bits;
      result |= (pBitBuffer->buffer[pBitBuffer->bytePosition] >> bitsRemainning) & mask(bits);
      printf("result:0x%x\n",result);
      bits = 0;
    }
    else {
      result <<= bitsRemainning;
      result |= pBitBuffer->buffer[pBitBuffer->bytePosition] & mask(bitsRemainning);
      printf("result:0x%x\n",result);
      bits -= bitsRemainning;
      pBitBuffer->bitPosition += bitsRemainning;
    }
    if (pBitBuffer->bitPosition == 8) {
      pBitBuffer->bitPosition = 0;
      bitsRemainning = 8;
      pBitBuffer->bytePosition++;
    }
  }
  return result;
}

//顺序向bitBuffer写bits位,其值为value

int bitBuffer_writeBits(tBitBuffer *pBitBuffer,unsigned int bits,unsigned int value) {

  unsigned int bitsRemainning = 8 - pBitBuffer->wBitPosition;
  while (bits != 0) {
    printf("bits:%d,bitPositon:%d, bitsRemainning:%d\n", bits, pBitBuffer->wBitPosition, bitsRemainning);
    if (bitsRemainning >= bits) {
      value <<= bitsRemainning - bits;
      pBitBuffer->buffer[pBitBuffer->wBytePosition] |= value;
      pBitBuffer->wBitPosition += bits;
      bitsRemainning -= bits;
      bits = 0;
      printf("L%d,value:0x%x, bits:%d, buffer[%d, 0x%x] \n",__LINE__, value, bits, pBitBuffer->wBytePosition, pBitBuffer->buffer[pBitBuffer->wBytePosition]);
    }
    else {
      pBitBuffer->buffer[pBitBuffer->wBytePosition] <<= bitsRemainning;
      pBitBuffer->buffer[pBitBuffer->wBytePosition] |= value >>(bits - bitsRemainning);
      value = value & mask(bits-bitsRemainning);
      bits -= bitsRemainning;
      pBitBuffer->wBitPosition += bitsRemainning;
      printf("L%d,value:%d, bits:%d, buffer[%d, 0x%x] \n",__LINE__, value, bits, pBitBuffer->wBytePosition, pBitBuffer->buffer[pBitBuffer->wBytePosition]);
    }
    if (pBitBuffer->wBitPosition == 8) {
      pBitBuffer->wBitPosition = 0;
      bitsRemainning = 8;
      pBitBuffer->wBytePosition++;
    }
  }
  printf("L%d,wBitPos:%d, wBytePos:%d \n",__LINE__, pBitBuffer->wBitPosition, pBitBuffer->wBytePosition);
  return 0;
}

//左移或右移bits位

int bitBuffer_shiftBits(tBitBuffer *pBitBuffer, int bits) {
  int absBits = bits > 0 ? bits : -bits;
  int shiftBytes = absBits / 8;
  int shiftBits = absBits % 8;
  int flag = bits > 0 ? 1 : 0;
  if ( flag ) {
    if (pBitBuffer->bitPosition + shiftBits > 8) {
      pBitBuffer->bytePosition += shiftBytes + 1;
      pBitBuffer->bitPosition += shiftBits -8;
    }
    else {
      pBitBuffer->bytePosition += shiftBytes ;
      pBitBuffer->bitPosition += shiftBits;
    }
  }
  else {
    if (pBitBuffer->bitPosition - shiftBits < 0) {
      pBitBuffer->bytePosition -= shiftBytes -1;
      pBitBuffer->bitPosition -= shiftBits -8;
    }
    else {
      pBitBuffer->bytePosition = shiftBytes ;
      pBitBuffer->bitPosition -= shiftBits ;

    }

  }
  if (pBitBuffer->bytePosition < 0 || pBitBuffer->bytePosition > 1024) {
    pBitBuffer->bytePosition = 0;
    pBitBuffer->bitPosition = 0;
    return -1;
  }
  else{
    return 0;
  }

}

//读取了多少位

int bitBuffer_getReadBits(tBitBuffer *pBitBuffer) {
  return 8 * pBitBuffer->bytePosition + pBitBuffer->bitPosition;
}

int bitBuffer_writeByte(tBitBuffer *pBitBuffer, unsigned char byte){
  pBitBuffer->buffer[pBitBuffer->wBytePosition ++] = byte;
  pBitBuffer->byteSize ++;
}

void main(void) {
  

  tBitBuffer *bitBuffer = (tBitBuffer*)malloc(sizeof(tBitBuffer));
  memset(bitBuffer, 0, sizeof(tBitBuffer));
  // bitBuffer_writeByte(bitBuffer, 0x56);
  // bitBuffer_writeByte(bitBuffer, 0xe2);
  bitBuffer_writeBits(bitBuffer, 11, 0x2b7);
  bitBuffer_writeBits(bitBuffer, 5, 0x2);

  unsigned long result = 0;

  bitBuffer_shiftBits(bitBuffer, 3);
  result = bitBuffer_readBits(bitBuffer, 6);
  int readBits = bitBuffer_getReadBits(bitBuffer);
  printf("result:0x%x, %d, bitPostion:%d, bytePosition:%d,byteSize:%d, wBytePosition:%d, readBits:%d\n", result,result, bitBuffer->bitPosition, bitBuffer->bytePosition, bitBuffer->byteSize, bitBuffer->wBytePosition,readBits );

  #if 0
  result = bitBuffer_readBits(&bitBuffer, 3);
  printf("result:0x%x, %d, bitPostion:%d, bytePosition:%d,byteSize:%d, wBytePosition:%d\n", result,result, bitBuffer.bitPosition, bitBuffer.bytePosition, bitBuffer.byteSize,   bitBuffer.wBytePosition);
  result = bitBuffer_readBits(&bitBuffer, 6);
  printf("result:0x%x, %d, bitPostion:%d, bytePosition:%d,byteSize:%d, wBytePosition:%d\n", result,result, bitBuffer.bitPosition, bitBuffer.bytePosition, bitBuffer.byteSize,   bitBuffer.wBytePosition);
  #endif
}

bit Buffer的更多相关文章

  1. Node.js:Buffer浅谈

    Javascript在客户端对于unicode编码的数据操作支持非常友好,但是对二进制数据的处理就不尽人意.Node.js为了能够处理二进制数据或非unicode编码的数据,便设计了Buffer类,该 ...

  2. java.IO输入输出流:过滤流:buffer流和data流

    java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...

  3. 一点公益商城开发系统模式Ring Buffer+

    一个队列如果只生产不消费肯定不行的,那么如何及时消费Ring Buffer的数据呢?简单的方案就是当Ring Buffer"写满"的时候一次性将数据"消费"掉. ...

  4. CSharpGL(38)带初始数据创建Vertex Buffer Object的情形汇总

    CSharpGL(38)带初始数据创建Vertex Buffer Object的情形汇总 开始 总的来说,OpenGL应用开发者会遇到为如下三种数据创建Vertex Buffer Object的情形: ...

  5. golang bytes.Buffer Reset

    func t() { a := []'} buf := new(bytes.Buffer) buf.Write(a) b := buf.Bytes() fmt.Println(b) buf.Reset ...

  6. 使用Ring Buffer构建高性能的文件写入程序

    最近常收到SOD框架的朋友报告的SOD的SQL日志功能报错:文件句柄丢失.经过分析得知,这些朋友使用SOD框架开发了访问量比较大的系统,由于忘记关闭SQL日志功能所以出现了很高频率的日志写入操作,从而 ...

  7. directx12中vetex buffer、index buffer和constant buffer绑定piple line的时机

    类别 时机 函数 建Heap vetex buffer 在Draw函数中 ID3D12GraphicsCommandList::IASetVertexBuffer 否 index buffer 在Dr ...

  8. JAVA NIO Buffer

    所谓的输入,输出,就是把数据移除或移入缓冲区.   硬件不能直接访问用户控件(JVM). 基于存储的硬件设备操控的是固定大小的数据块儿,用户请求的是任意大小的或非对齐的数据块儿.   虚拟内存:使用虚 ...

  9. Circular Buffer

    From:http://bradforj287.blogspot.com/2010/11/efficient-circular-buffer-in-java.html import java.util ...

  10. Buffer类

    输入流中可以通过缓冲区来加大读取的效率,sun公司感觉可以加快执行效率,他就为我们提供了一个类来操作缓存区. Buffer来头的类:所有缓冲流都是以Buffer开头的: 学习缓冲流的作用: Buffe ...

随机推荐

  1. linux命令解压压缩rar文件的详细步骤

    参考文件:https://www.cnblogs.com/qinglin/p/9007939.html

  2. numpy 一些知识

    import numpy as np 什么类型的相加,返回的还是什么类型的,所以在累加小类型的数值时会出现问题如下: a=np.array([123,232,221], dtype=np.uint8) ...

  3. AntDesign(React)学习-14 使用UMI提供的antd模板

    1.UMI提供了可视化antd模板,可以直接添加到项目中修改用 比如将个人中心添加到项目中 2.选择个人中心,确定 3.成功 4.打开项目 5.Route文件也自动添加 根路由有exact:true后 ...

  4. 谷歌浏览器安装apizza

    接口测试是卡发中不可缺少的一环,那么常用的postman是英文就很不方便一些小伙伴的使用,所以此工具和postman功能相同且升级并且是中文模式 安装: 链接:https://pan.baidu.co ...

  5. win10中批量新建文件夹

    1.新建一个bat文件,如[批量新建.bat].或者新建txt文件,输入完内容后重命名为bat文件 2.建议用notepad软件打开文件,首先确定编码格式为ANSI编码 (否则最后出现的效果是乱码,不 ...

  6. Spring boot --- 自动配置

    spring boot 自动配置 指的是针对很多spring 应用程序常见的应用功能,spring boot 能自动提供相关配置. spring boot 自动配置加载     Spring boot ...

  7. 转载:android audio flinger

    https://blog.csdn.net/innost/article/details/6142812 https://blog.csdn.net/zyuanyun/article/details/ ...

  8. VNCserver 安装 及 oracle过程总结

    一.安装桌面系统 1.命令   yum grouplist ---列举系统中以组安装的包(组安装的包会包括很多,组安装一下就就可以安装很多附在的包.) 2.  yum groupinstall &qu ...

  9. Apache 安装概要

    1.apache下载参照百度 bin文件夹下命令行:  httpd -k install 2.安装完成后排错记录 服务无法启动,到bin目录下运行  httpd.exe  查看输出,然后百度一下输出即 ...

  10. session的到底是做什么的?

    原文地址:https://blog.csdn.net/h19910518/article/details/79348051 前言: 今天就来彻底的学一些session是个啥东西,我罗列了几个需要知道的 ...