Sign extension for data types with arbitrary bit sizes (C#)

02/01/2013 09:43 Lateralus#1
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 InfamousNoone#2
new encryption, more important things to care about, u wasted an hour on this, sorry babe <3
02/01/2013 10:57 Super Aids#3
Quote:
Originally Posted by InfamousNoone View Post
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 Lateralus#4
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 CptSky#5
Quote:
Originally Posted by Lateralus View Post
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 :p (I work a lot on file format, compression, etc. So, might be useful at some point)