This repository was archived by the owner on Aug 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathInt48.cs
79 lines (69 loc) · 2.15 KB
/
Int48.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BitStreams
{
/// <summary>
/// Represents a 48-bit signed integer
/// </summary>
[Serializable]
public struct Int48
{
private byte b0, b1, b2, b3, b4, b5;
private Bit sign;
private Int48(long value)
{
this.b0 = (byte)(value & 0xFF);
this.b1 = (byte)((value >> 8) & 0xFF);
this.b2 = (byte)((value >> 16) & 0xFF);
this.b3 = (byte)((value >> 24) & 0xFF);
this.b4 = (byte)((value >> 32) & 0xFF);
this.b5 = (byte)((value >> 40) & 0x7F);
this.sign = (byte)((value >> 47) & 1);
}
public static implicit operator Int48(long value)
{
return new Int48(value);
}
public static implicit operator long (Int48 i)
{
long value = i.b0 + (i.b1 << 8) + (i.b2 << 16) + ((long)i.b3 << 24) + ((long)i.b4 << 32) + ((long)i.b5 << 40);
return -((long)i.sign << 47) + value;
}
public Bit GetBit(int index)
{
return (byte)(this >> index);
}
}
/// <summary>
/// Represents a 48-bit unsigned integer
/// </summary>
[Serializable]
public struct UInt48
{
private byte b0, b1, b2, b3, b4, b5;
private UInt48(ulong value)
{
this.b0 = (byte)(value & 0xFF);
this.b1 = (byte)((value >> 8) & 0xFF);
this.b2 = (byte)((value >> 16) & 0xFF);
this.b3 = (byte)((value >> 24) & 0xFF);
this.b4 = (byte)((value >> 32) & 0xFF);
this.b5 = (byte)((value >> 40) & 0xFF);
}
public static implicit operator UInt48(ulong value)
{
return new UInt48(value);
}
public static implicit operator ulong (UInt48 i)
{
ulong value = (i.b0 + ((ulong)i.b1 << 8) + ((ulong)i.b2 << 16) + ((ulong)i.b3 << 24) + ((ulong)i.b4 << 32) + ((ulong)i.b5 << 40));
return value;
}
public Bit GetBit(int index)
{
return (byte)(this >> index);
}
}
}