This repository has been 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 pathInt24.cs
73 lines (63 loc) · 1.68 KB
/
Int24.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace BitStreams
{
/// <summary>
/// Represents a 24-bit signed integer
/// </summary>
[Serializable]
public struct Int24
{
private byte b0, b1, b2;
private Bit sign;
private Int24(int value)
{
this.b0 = (byte)(value & 0xFF);
this.b1 = (byte)((value >> 8) & 0xFF);
this.b2 = (byte)((value >> 16) & 0x7F);
this.sign = (byte)((value >> 23) & 1);
}
public static implicit operator Int24(int value)
{
return new Int24(value);
}
public static implicit operator int (Int24 i)
{
int value = (i.b0 | (i.b1 << 8) | (i.b2 << 16));
return -(i.sign << 23) + value;
}
public Bit GetBit(int index)
{
return (this >> index);
}
}
/// <summary>
/// Represents a 24-bit unsigned integer
/// </summary>
[Serializable]
public struct UInt24
{
private byte b0, b1, b2;
private UInt24(uint value)
{
this.b0 = (byte)(value & 0xFF);
this.b1 = (byte)((value >> 8) & 0xFF);
this.b2 = (byte)((value >> 16) & 0xFF);
}
public static implicit operator UInt24(uint value)
{
return new UInt24(value);
}
public static implicit operator uint (UInt24 i)
{
return (uint)(i.b0 | (i.b1 << 8) | (i.b2 << 16));
}
public Bit GetBit(int index)
{
return (byte)(this >> index);
}
}
}