Page 1 of 1

fun with big numbers

Posted: Sun Aug 05, 2007 10:13 pm
by Red Squirrel
I wish my college thought us about AND OR XOR and such. I'm having too much fun here.

Code: Select all

void RSbitstream::WriteInt32(unsigned int tw)
{
if(tw>0xFFFFFFFF)tw=0xFFFFFFFF;

stream.Add(((2147483648UL & tw)==2147483648UL) ? true : false); 
stream.Add(((1073741824 & tw)==1073741824) ? true : false); 
stream.Add(((536870912 & tw)==536870912) ? true : false); 
stream.Add(((268435456 & tw)==268435456) ? true : false); 
stream.Add(((134217728 & tw)==134217728) ? true : false); 
stream.Add(((67108864 & tw)==67108864) ? true : false); 
stream.Add(((33554432 & tw)==33554432) ? true : false); 
stream.Add(((16777216 & tw)==16777216) ? true : false); 

stream.Add(((8388608 & tw)==8388608) ? true : false); 
stream.Add(((4194304 & tw)==4194304) ? true : false); 
stream.Add(((2097152 & tw)==2097152) ? true : false); 
stream.Add(((1048576 & tw)==1048576) ? true : false); 
stream.Add(((524288 & tw)==524288) ? true : false); 
stream.Add(((262144 & tw)==262144) ? true : false); 
stream.Add(((131072 & tw)==131072) ? true : false); 
stream.Add(((65536 & tw)==65536) ? true : false); 

stream.Add(((32768 & tw)==32768) ? true : false); 
stream.Add(((16384 & tw)==16384) ? true : false); 
stream.Add(((8192 & tw)==8192) ? true : false); 
stream.Add(((4096 & tw)==4096) ? true : false); 
stream.Add(((2048 & tw)==2048) ? true : false); 
stream.Add(((1024 & tw)==1024) ? true : false); 
stream.Add(((512 & tw)==512) ? true : false); 
stream.Add(((256 & tw)==256) ? true : false); 

stream.Add(((128 & tw)==128) ? true : false); 
stream.Add(((64 & tw)==64) ? true : false); 
stream.Add(((32 & tw)==32) ? true : false); 
stream.Add(((16 & tw)==16) ? true : false); 
stream.Add(((8 & tw)==8) ? true : false); 
stream.Add(((4 & tw)==4) ? true : false); 
stream.Add(((2 & tw)==2) ? true : false); 
stream.Add(((1 & tw)==1) ? true : false); 
}
I could probably make a loop for that... lol

Archived topic from AOV, old topic ID:1050, old post ID:6897

fun with big numbers

Posted: Mon Aug 06, 2007 12:08 am
by Red Squirrel
Yay for improved and more versatile code:

Code: Select all

//writes a int based on specified lenght (bits)
void RSbitstream::WriteVarInt(unsigned int tw,int bits)
{
if(bits>32)bits=32;
if(bits<1)bits=1;

	unsigned long int orer=1;	
	for(int i=1;i<bits;i++)
	{
	orer*=2;
	}
	
	//make sure tw is not past its capacity
	tw = (tw>((orer*2)-1)) ? ((orer*2)-1) : tw;
	
	for(int i=0;i<bits;i++)
	{
	stream.Add(((orer & tw)==orer) ? true : false); 
	
	orer/=2;
	}

}

Believe it or not, all this is leading up to me continuing to work on my UO client. These functions will make it easier to deal with network data.

Archived topic from AOV, old topic ID:1050, old post ID:6902

fun with big numbers

Posted: Mon Aug 06, 2007 2:14 pm
by Lymus
It was fun having you explain to me that crap yesterday. I was kinda like "Huh?"

Archived topic from AOV, old topic ID:1050, old post ID:6925

fun with big numbers

Posted: Thu Aug 09, 2007 10:12 am
by Red Squirrel
Hahah yeah, the 2nd code is actually the variable lenght version of the first. I can specity the number of bits. Converts a number to binary, basically. There's also a ReadVarInt version which does the oposite, takes a stream of bits and returns a number.

Archived topic from AOV, old topic ID:1050, old post ID:7038

fun with big numbers

Posted: Thu Aug 09, 2007 4:22 pm
by Lymus
Red Squirrel wrote:Hahah yeah, the 2nd code is actually the variable lenght version of the first. I can specity the number of bits. Converts a number to binary, basically. There's also a ReadVarInt version which does the oposite, takes a stream of bits and returns a number.
There's only 10 types of people in this world. Those who know binary, and those who don't.

Archived topic from AOV, old topic ID:1050, old post ID:7046