Jump to content

  • Log In with Google      Sign In   
  • Create Account

vNeeki

Member Since 16 Jul 2010
Offline Last Active Sep 05 2012 04:15 AM

#4917451 required bit count at compile time

Posted by vNeeki on 28 February 2012 - 12:08 PM


HDD/RAM space is not a problem these days


Who said anything about storage? Believe it or not, there are still some parts of the world where network bandwidth is not abundant.


but you could use one bit flag to tell whether to read a full byte or 4bits


I'm not sure how this would help me. I want to save minimum number of bits required. It could be 2, 4, 7 or even 23. I just wanted to know, if anybody had a quick idea how to get number of minimum bits required based on some max value (at compile time).


struct Value
{
	int val;
	int bits;
};

static const Values[16] =
{
	{0,1},
	{1,1},
	{2,2},
	{3,2},
	{4,3},
	{5,3},
	{6,3},
	{7,3},
	{8,4},
	{9,4},
	{10,4},
	{11,4},
	{12,4},
	{13,4},
	{14,4},
	{15,4},
};

void MyWriter::WriteByte(unsigned char Byte)
{
	if(Byte < 16)  
	{
		WriteBit(1);
		WriteBits(Values[Byte].value,Values[Byte].bits);
	}
	else
	{
		WriteBit(0);
		WriteBits(Byte,8);
	}
}

void MyWriter::WriteWord(unsigned short Word)
{
	WriteByte(Word >> 8);
	WriteByte(Word & 0xFF);
}

D:


#4917415 required bit count at compile time

Posted by vNeeki on 28 February 2012 - 10:23 AM

HDD/RAM space is not a problem these days but you could use one bit flag to tell whether to read a full byte or 4bits.


#4914501 Putting data into a char* or std::string

Posted by vNeeki on 19 February 2012 - 08:16 AM


int WriteBinary(FILE* fp,uint64_t value)
{
	return fwrite(&value,1,8,fp);
}

int WriteBinary(FILE* fp,const char* value)
{
	return fwrite(value,1,strlen(value),fp);
}

int WriteBinary(FILE* fp,char* value)
{
	return WriteBinary(fp,(const char*)value);
}

int WriteBinary(FILE* fp,void* d,uint32_t l)
{
	return fwrite(d,1,l,fp);
}

int WriteBinary(FILE* fp,const string& value)
{
	return WriteBinary(fp,(const char*)value.c_str());
}

int main()
{
	FILE* fp = fopen("file","wb");
	double d = 1234.56789;
	float f = 1234.1234f;
	int i = 1234;
	long long int l = 99999999;
	const char* s = "gggggg";
	string ss = "ccccc";
	const char* sss = "????";

	WriteBinary(fp,d);
	WriteBinary(fp,f);
	WriteBinary(fp,i);
	WriteBinary(fp,l);
	WriteBinary(fp,s);
	WriteBinary(fp,ss);
	WriteBinary(fp,sss,strlen(sss));
	fclose(fp);
	return 0;
}

What do i win?

Edit1 : if you would like to store strings first write their length followed by the literal.


PARTNERS