Skip to content

Instantly share code, notes, and snippets.

@stravant
Created November 6, 2012 03:56
Show Gist options
  • Save stravant/4022455 to your computer and use it in GitHub Desktop.
Save stravant/4022455 to your computer and use it in GitHub Desktop.
Packed Restaurant
class PackedRestaurantRecord {
public:
//MAD BIT TWIDDLING ~~~ AVERT YOUR GAZE!
uint8_t getBlockNum() {
return (C & 0x0F) | (D & 0xF0);
}
void setBlockNum(uint8_t n) {
C = (C & 0xF0) | (n & 0x0F);
D = (D & 0x0F) | (n & 0xF0);
}
uint8_t getBlockInx() {
return (C & 0x70) >> 4;
}
void setBlockInx(uint8_t i) {
C = (C & 0x8F) | ((i & 0x07) << 4);
}
uint32_t getDistance() {
int32_t a = A;
int32_t b = B;
return a | (b << 8) | ((C & 0x80) ? 0x00010000 : 0x0);
}
void setDistance(uint32_t d) {
A = (d & 0x000000FF);
B = (d & 0x0000FF00) >> 8;
C &= 0x7F;
if (d & 0x00010000)
C |= 0x80;
}
uint8_t getRating() {
return D & 0x0F;
}
void setRating(uint8_t r) {
D = (D & 0xF0) | (r & 0x0F);
}
private:
//
// | BlockNum | BlockInx | Distance | Rating |
// N I D R
//
// MOST SIGNIGICANT:
// -> Stored as: DDDD DDDD DDDD DDDD DIII NNNN NNNN RRRR
// A B C D
uint8_t A, B, C, D;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment