bit_file->rack |=bit_file->mask;
补充:上面语句中的 |= 是什么意思?
完整源代码:
/*************开始BITIO.C**********/
/*
*该文件包含了所有的存取BIT的函数,适用于ANSI或者K&R C
*本书的每个程序都必须引用它
*/
#include <stdio.h>
#include <stdlib.h>
#include “bitio.h”
#include “errhand.h”
BIT_FILE *OpenOutputBitFile( name )
char *name;
{
BIT_FILE *bit_file;
bit_file = (BIT_FILE *) calloc(1,sizeof( BIT_FILE ));
if ( bit_file == NULL )
return( bit_file );
bit_file->file=fopen( name,”rb” );
bit_file->rack = 0;
bit_file->mask = 0×80;
bit_file->pacifier_counter = 0;
return( bit_file );
}
BIT_FILE *OpenInputBitFile( name )
char *name;
{
BIT_FILE *bit_file;
bit_file=(BIT_FILE *)calloc(1,sizeof( BIT_FILE ));
if( bit_file == NULL )
return( bit_file );
bit_file->file=fopen( name,”rb” );
bit_file->rack=0;
bit_file->mask=0×80;
bit_file->pacifier_counter=0;
return( bit_file );
}
void CloseOutputBitFile( bit_file )
BIT_FILE *bit_file;
{
if( bit_file->mask != 0×80 )
if( putc( bit_file->rack, bit_file->file )!=bit_file->rack )
fatal_error( “关闭Bit文件时发生致命错误!\n” );
fclose( bit_file->file );
free((char *)bit_file);
}
void CloseInputBitFile( bit_file )
BIT_FILE *bit_file;
{
fclose( bit_file->file );
free((char *)bit_file );
}
void OutputBit( bit_file,bit )
BIT_FILE *bit_file;
int bit;
{
if(bit)
bit_file->rack |=bit_file->mask;
bit_file->mask>>=1;
if( bit_file->mask==0){
if(putc(bit_file->rack,bit_file->file )!=bit_file->rack)
fatal_error(“在OutputBit处发生致命错误!\n”);
else
if((bit_file->pacifier_counter++ & 4095 )==0)
putc(‘.’,stdout);
bit_file->rack=0;
but_file->mask=0×80;
}
}
int InputBit( bit_file )
BIT_FILE *bit_file;
{
int value;
if( bit_file->mask==0×80 )
{
bit_file->rack=getc( bit_file->file );
if(bit_file->rack==EOF)
fatal_error(“在InputBit里发生致命错误!\n”);
if(( bit_file->pacifier_counter++&2047)==0)
putc(‘.’,stdout);
}
value=bit_file->rack & bit_file->mask;
bit_file->mask>>=1;
if( bit_file->mask=0)
bit_file->mask=0×80;
return (value?1:0);
}
unsigned long InputBits( bit_file,bit_count )
BIT_FILE *bit_file;
int bit_count;
{
unsigned long mask;
unsigned long return_value;
mask=1L<<(bit_count-1);
return_value=0;
while(mask!=0)
{
if(bit_file->mask==0×80)
{
bit_file->rack=getc( bit_file->file);
if(bit_file->rack==EOF)
fatal_error(“在InputBit里发生致命错误!\n”);
if((bit_file->pacifier_counter++ & 2047)==0)
putc(‘.’,stdout);
}
if(bit_file->rack&bit_file->mask)
return_value |=mask;
mask>>=1;
bit_file->mask>>=1;
if(bit_file->mask=00)
bit_file->mask=0×80;
}
return (return_value);
}
void FilePrintBinary( file,code,bits )
FILE *file;
unsigned int code;
int bits;
{
unsigned int mask;
mask=1<<(bits-1);
while(mask!=0)
{
if(code&mask)
fputc(’1′,file);
else
fputc(’0′,file);
mask>>=1;
}
}
/********************结束 BITIO.C************/
>> 本文固定链接: http://www.vcgood.com/archives/2405