-
Notifications
You must be signed in to change notification settings - Fork 1
/
Int24.h
67 lines (56 loc) · 1.4 KB
/
Int24.h
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
#pragma once
struct uint24_t
{
// Explicit getter returning native type. Converts from file representation,
// which is little-endian.
uint32_t Get() const throw()
{
return
(static_cast<uint32_t>(value[2]) << 16) |
(static_cast<uint32_t>(value[1]) << 8) |
value[0];
}
// Implicit conversion to native type.
operator uint32_t() const throw()
{
return Get();
}
void Set(uint32_t v) throw()
{
value[0] = uint8_t(v);
value[1] = uint8_t(v >> 8);
value[2] = uint8_t(v >> 16);
}
uint24_t& operator =(uint32_t v) throw()
{
Set(v);
return *this;
}
uint8_t value[3];
};
struct int24_t
{
// Explicit getter returning native type. Converts from file representation,
// which is little-endian.
int32_t Get() const throw()
{
return value.Get();
}
// Implicit conversion to native type.
operator int32_t() const throw()
{
// Get the 32-bit value from 24-bits, then extend sign.
uint32_t integerValue = value.Get();
return int32_t(integerValue << 8) >> 8;
}
void Set(int32_t v) throw()
{
value.Set(v);
}
int24_t& operator =(int32_t v) throw()
{
value.Set(v);
return *this;
}
uint24_t value;
};