指数哥伦布编码

头文件

#ifndef __COLUMBU_UE_H__
#define __COLUMBU_UE_H__

#include <stdio.h>
#include <string.h>

typedef struct Bits_Operation {
	unsigned char (*read_bit)(const char *arr, int *BitPosition, int *BytePosition);
} BITS_OPS, *P_BITS_OPS;

typedef struct Columbu_Operation {
	int (*get_ue)(const char *arr, int *BitPosition, int *BytePosition);
} COLUMBU_OPS, *P_COLUMBU_OPS;

P_COLUMBU_OPS columbufops = NULL;
P_BITS_OPS bitsfops = NULL;

#endif

主文件

#include "columbu_ue.h"

/* 读取单个字节中的位 */
static unsigned char read_bit(const char *arr, int *BitPosition, int *BytePosition)
{
	int val = (arr[*BytePosition] & (1 << (7 - *(BitPosition)))) == 0 ? 0 : 1;
	
	if(*BitPosition < 7) {
		(*BitPosition)++;	
	} else {
		*BitPosition = 0;
		(*BytePosition)++;
	}

	return val;
}

/* 无符号指数哥伦布编码 */
static int get_columbu_ue(const char *arr, int *BitPosition, int *BytePosition)
{	
	unsigned int val = 0;
	unsigned char Bit;
	unsigned int PrefixCount = 0;
	unsigned int Prefix = 0,Suffix = 0;
	
	while(1) {
		Bit = bitsfops->read_bit(arr,BitPosition,BytePosition);
		if(Bit == 0) {
			PrefixCount++;
		} else {
			break;
		} 
	}

	Prefix = (1 << PrefixCount) - 1;
	for(int i = 0; i < PrefixCount; i++) {
		Bit = bitsfops->read_bit(arr,BitPosition,BytePosition);
		Suffix += Bit * (1 << (PrefixCount - i - 1));
	}
	
	val = Prefix + Suffix;
	
	return val;
}

BITS_OPS bits_fops = {
	.read_bit = read_bit,
};

COLUMBU_OPS columbu_fops = {
	.get_ue = get_columbu_ue,
};

int main(void)
{	
	unsigned char arr[6] = {0xA6,0x42,0x98,0xE2,0x04,0x8A};
	int Val = 0;
	int BitPosition = 0;
	int BytePosition = 0;
	int BitLength = sizeof(arr) / sizeof(unsigned char) * 8;
	
	bitsfops = &bits_fops;
	columbufops = &columbu_fops;	
	
	while(BytePosition * 8 + BitPosition < BitLength)  {
		Val = columbufops->get_ue(arr,&BitPosition,&BytePosition);
		printf("get_ue = %d\n",Val);
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值