|
You last visited: Today at 16:45
Advertisement
Sign extension for data types with arbitrary bit sizes (C#)
Discussion on Sign extension for data types with arbitrary bit sizes (C#) within the CO2 Programming forum part of the Conquer Online 2 category.
02/01/2013, 09:43
|
#1
|
elite*gold: 0
Join Date: May 2005
Posts: 1,892
Received Thanks: 918
|
Sign extension for data types with arbitrary bit sizes (C#)
I needed this functionality for map compression (height and validity to take up 2 bytes rather than 3 bytes), which would have been much easier if the height wasn't signed. I did some research about sign extension, so here's a cool little method allowing for sign extension on a data type of any bit size less than 8 bytes, rather than a byte power of 2. This method also allows for truncation to any primitive numerical data type giving the same result. It treats the most significant bit as the sign. It's also optimized and has an xml comment because yolo swag.
Code:
/// <summary>
/// Sign extends a value of the specified size (in bits).
/// </summary>
/// <param name="value">The value to sign extend.</param>
/// <param name="bitSize">The size of the value in bits.</param>
/// <returns>A sign extended value.</returns>
long SignExtend(long value, int bitSize)
{
int counterBitSize = (sizeof(long) << 3) - bitSize;
return (value & (long.MaxValue >> (counterBitSize - 1))) << counterBitSize >> counterBitSize;
}
If generics allowed for a constraint on only numerical types allowing for the sizeof operation and exposing MaxValue and MinValue, this could be perfect .
Note: You could hardcode 64 in place of sizeof(long) << 3, but I'm not sure if data type sizes vary. Also, I'd make an overload to use ints instead of long as it's more optimized for 32-bit systems.
|
|
|
02/01/2013, 09:45
|
#2
|
elite*gold: 20
Join Date: Jan 2008
Posts: 2,012
Received Thanks: 2,882
|
new encryption, more important things to care about, u wasted an hour on this, sorry babe <3
|
|
|
02/01/2013, 10:57
|
#3
|
elite*gold: 0
Join Date: Dec 2012
Posts: 1,761
Received Thanks: 946
|
Quote:
Originally Posted by InfamousNoone
new encryption, more important things to care about, u wasted an hour on this, sorry babe <3
|
You just killed him
|
|
|
02/01/2013, 11:30
|
#4
|
elite*gold: 0
Join Date: May 2005
Posts: 1,892
Received Thanks: 918
|
Haha, honestly there's really no one in this community that I can think of who could get use out of this. The map compression that I'm doing is a rare exception, especially since 99% of private servers don't account or check for height anyway. Almost everything else in conquer can be unsigned, and you could use simple shifts and masks if you want to compress/decompress something. Point is, I may not have wasted an hour on coding it, but I wasted my time sharing it. But hey, maybe someone can find another weird use for it.
|
|
|
02/01/2013, 15:24
|
#5
|
elite*gold: 0
Join Date: Jan 2008
Posts: 1,434
Received Thanks: 1,147
|
Quote:
Originally Posted by Lateralus
Haha, honestly there's really no one in this community that I can think of who could get use out of this. The map compression that I'm doing is a rare exception, especially since 99% of private servers don't account or check for height anyway. Almost everything else in conquer can be unsigned, and you could use simple shifts and masks if you want to compress/decompress something. Point is, I may not have wasted an hour on coding it, but I wasted my time sharing it. But hey, maybe someone can find another weird use for it.
|
Well, I reimplemented the .NET packed integer in C++... So, I might use your thing for weird things also (I work a lot on file format, compression, etc. So, might be useful at some point)
|
|
|
|
Similar Threads
|
dragon event sizes~
08/23/2011 - Grand Chase Philippines - 10 Replies
can anyone post a picture of the large version of the dragon acce. here?
|
[RELEASE] General Data Types.
05/08/2010 - CO2 PServer Guides & Releases - 11 Replies
This is just a tiny release with the correct sub-types for the General Data .
This will be a part of the things I will be dumping after I formatted my PC without being prepared. I'm sorry for all other info I still had on the CO2 PServer development, it is not available for my any more. I was going to release it all though, but not just yet.
NinjaStep = 0x9C,
EndFly = 0x78,
GUIDialog = 0x7E,
SetLocation = 0x4A,
SetMapColor = 0x68,
Jump = 0x89,
|
help with sizes and pixels
01/08/2007 - Conquer Online 2 - 2 Replies
Could anybody help me by telling me how do i resize the monster size? I mean any monster? Thank you in advance.
|
Font In-Game sizes
10/27/2005 - Conquer Online 2 - 2 Replies
:eek: Look i'm partially BLIND. Well I wanna make the in game fonts bigger. Outta all the brains in this site, gotta be sum1 that will do this for the other who need it. Just need me to bring up the topic ;)
|
All times are GMT +2. The time now is 16:45.
|
|