FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Topics & Contributions
   Information Visualization
(Moderators: forkinsocket, REAS)
   Bit Visualizer
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Bit Visualizer  (Read 1117 times)
rgovostes

rgovostes
Bit Visualizer
« on: Mar 11th, 2004, 12:25am »

Here's a simple number visualizer - it converts an integer into a 1x32 pixel image, and places many of these images next to one another to create a nifty pattern. In addition, it scrolls this pattern so new numbers are always being shown.
 
I threw this together in a few minutes - I was surprised that my method of extracting bit values from an integer worked, since I devised it using some fuzzy logic.
 
The code below will show you the pattern for the triangular number series (n + n-1 + n-2 ... 1). You can change what it shows by simply changing the "calculate" function. I like the expression "int(atan2(num, offset) * 2147483647)" - it looks as if pyramids emerge on the bottom. If you come up with something cool, please share!
 
It is largely unoptimized. I wrote it quickly to try something out. Improvements are welcome, but I probably won't be updating it myself.
 
Code:
// Bit Visualizer v1, Mar 10 '04
// by Ryan Govostes
 
int offset = 0;
 
void setup() {
  size(512, 32); // 32 = 8**4 (size of an integer)
}
 
void loop() {
  paint();
  offset ++;
}
 
void paint() {
  int y, x, n;
  background(255);
  for(x = offset; x < (width + offset); x ++) {
    n = calculate(x);
    for(y = 0; y < height; y ++) {
      if(n % pow(2, y) > 0) set((x - offset), y, color(0, 0, 0));
      n -= n % pow(2, y);
    }
  }
}
 
int calculate(int num) {
  return int(0.5 * num * (num - 1)); // Compute "triangle number"
}
« Last Edit: Mar 11th, 2004, 12:28am by rgovostes »  
arielm

WWW
Re: Bit Visualizer
« Reply #1 on: Mar 11th, 2004, 3:44am »

off-topic: it reminded me of the msx & spectrum days!
 

Ariel Malka | www.chronotext.org
Mythmon


Re: Bit Visualizer
« Reply #2 on: Mar 13th, 2004, 6:37am »

A simple way of extracting bit values from decimal number is something like this
(did this right now so, meh)
Code:

int decToBit(int x) {
  int number=0;
  for (int i=0; i<8; i++) {
    if (x>=(pow(2,i))) {
 number += 1*(max(pow(10,i),1));
 x -= pow(2,i);
    }
  }
  return number;
}

 
of course you would probaly want to change the for loop to repeat 32 times, and replace the number += 1*(max(pow(10,i),1)); line to something like set(someX,i,0x000000) or something.
 
not even sure if this is what you were trying to do with that function, but you could probaly achieve the same results this way as well.
 
fry

WWW
Re: Bit Visualizer
« Reply #3 on: Mar 13th, 2004, 8:22pm »

bit shifting also gives you better numbers than pow(), since pow() will return floats and bit shifts will be ints.
 
for instance: (1 << 2) means 1 shifted two places to the left, effectively 2 to the 2nd power (1 << 5 would be 2 to the 5th). so if you wanted to unpack an int:
Code:
int mynum = 2717; // num to unpack
for (int i=31; i >= 0; --i) {  // start at 31, go down to 0
  int bit = 1 << i;
  if ((mynum & bit) == 1) {
    print("1");
  } else {
    print("0");
  }
}

the & means "logical and", and has been discussed elsewhere on the board with regards to color.  
 
there are even geekier/fancier methods to unpacking the numbers (i realize i'm only baiting toxi to implement this in 2 or 3 lines, which can be done) but i'll spare you..
 
kevinP

Email
Re: Bit Visualizer
« Reply #4 on: Mar 14th, 2004, 8:04pm »

Hi,
 
What are these two things doing here? Trying mython's I get something like "11111111" (I don't recall how many) for 2717 and using Ben's I get "00000000000000000000000000000001".
 
I thought that unpacking an integer meant converting it to binary, but I think I missed something here.
 
-K (thanks in advance)
 

Kevin Pfeiffer
fry

WWW
Re: Bit Visualizer
« Reply #5 on: Mar 14th, 2004, 9:51pm »

whups, bug in there.. this one works:
 
Code:
// there are more elegant ways to do this..
int mynum = 2717; // num to unpack  
for (int i=31; i >= 0; --i) {  // start at 31, go down to 0  
  int bit = 1 << i;  
  if ((mynum & bit) == bit) {  
    print("1");  
  } else {  
    print("0");  
  }  
}

 
also, if you don't need all 32 digits, you needn't start the loop at 31 down to 0, you could just go 15..0 or 7..0 for either 16 or 8 digits.
 
finally, there's a java method to do it, though it'll only print as many digits as needed, rather than a fixed width:
 
println(Integer.toBinaryString(mynum));
« Last Edit: Mar 14th, 2004, 9:54pm by fry »  
TomC

WWW
Re: Bit Visualizer
« Reply #6 on: Mar 14th, 2004, 11:07pm »

on Mar 13th, 2004, 8:22pm, fry wrote:
there are even geekier/fancier methods to unpacking the numbers

 
I'll bite.  This does the same, I think.
 
Code:

int mynum = 2717; // num to unpack  
for (int i=31; i >= 0; --i) {
  print(mynum >> i & 1);  
}

 
rgovostes

rgovostes
Re: Bit Visualizer
« Reply #7 on: Mar 15th, 2004, 10:05pm »

Much faster, thanks TomC!
 
Fry, I didn't know whether or not Processing/Java supported bitwise operations, so I came up with my own system.
 
Code:
// Bit Visualizer v1.5, Mar 15 '04
// by Ryan Govostes
 
int offset = 0;
 
void setup() {
  size(512, 32); // 32 = 8**4 (size of an integer)
}
 
void loop() {
  paint();
  offset ++;
}
 
void paint() {
  int y, x, n;
  background(255);
  for(x = offset; x < (width + offset); x ++) {
    n = calculate(x);
    for(y = 31; y >= 0; y --) {
      if((n >> y & 1) == 1) set((x - offset), y, color(0, 0, 0)); // Thanks, TomC!
    }
  }
}
 
int calculate(int num) {
  return int(0.5 * num * (num - 1)); // Compute "triangle number"
}

 
Here's an implementation of the Fibonacci series (since it can only be displayed 32 pixels tall, it doesn't generate a very good picture ... see here). It is a mediocre example of how to show data that depends on previously calculated values.
 
Code:
int a, b, oldoffset;
int calculate(int num) {
  int i, c = 1;
  
  if (offset > oldoffset) {
    a = 0;
    b = 1;
    for(i = 0; i < offset; i ++) {
 c = a + b;
 a = b;
 b = c;
    }
    oldoffset = offset;
  } else {
    c = a + b;
    a = b;
    b = c;
  }
  return c;
}
« Last Edit: Mar 16th, 2004, 10:24pm by rgovostes »  
Pages: 1 

« Previous topic | Next topic »