Skip to content

Instantly share code, notes, and snippets.

@2bbb
Created June 18, 2015 10:29
Show Gist options
  • Select an option

  • Save 2bbb/1f200b94c6b58b457d39 to your computer and use it in GitHub Desktop.

Select an option

Save 2bbb/1f200b94c6b58b457d39 to your computer and use it in GitHub Desktop.
binary_data_study
#include "ofMain.h"
class ofApp : public ofBaseApp {
struct hex_info {
bitset<32> bit_seq;
string str_form;
};
deque<hex_info> lines;
union {
uint32_t input;
float output;
} value;
int speed;
uint64_t current;
public:
void setup() {
ofEnableSmoothing();
ofEnableAlphaBlending();
speed = 20;
current = 0;
}
void make_bit_seq(uint32_t v, bitset<32> &bs) {
for(int i = 0; i < 32; i++) {
if(((v >> (31 - i)) & 1)) bs.set(i);
}
}
string print_bit_seq(bitset<32> &bs) {
ostringstream ss("");
for(int i = 0; i < 32; i++) ss << (int)bs.test(i);
return ss.str();
}
void update() {
for(int i = 0; i < speed; i++) {
value.input = current++;
hex_info info;
make_bit_seq(value.input, info.bit_seq);
info.str_form = ofVAArgsToString("= 0b%s = 0%010d = 0x%08X -> %090.45f\n", print_bit_seq(info.bit_seq).c_str(), value.input, value.input, value.output);
lines.push_back(info);
if(88 < lines.size()) {
lines.pop_front();
}
}
}
void draw() {
ofBackground(0);
ofTranslate(140, 0);
for(int i = 0; i < lines.size(); i++) {
for(int j = 0; j < 32; j++) {
float x = ofMap(j, 0, 32, 0, 384);
ofSetColor(lines[i].bit_seq.test(j) ? 255 : 64);
ofRect(x + 1, 12 + i * 12, 10, 10);
}
ofSetColor(255);
ofDrawBitmapString(lines[i].str_form, 392, 20 + i * 12);
}
}
void keyPressed(int key) {
}
};
int main() {
ofSetupOpenGL(1920, 1080, OF_WINDOW);
ofRunApp(new ofApp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment