ASCII and Bits

Posted by SkidRunner on Nov. 24, 2015, 1:54 p.m.

Hello all it has again been a while since the last time I did any development but I did stumble on something that made me giggle.

{ 0x7E, 0x81, 0xA5, 0x81, 0xBD, 0x99, 0x81, 0x7E } = smile

I know this seems strange and maybe makes no sense but here is my explanation.

So if we convert all the bytes above into bits we get the following results.

0x7E = 01111110
0x81 = 10000001
0xA5 = 10100101
0x81 = 10000001
0xBD = 10111101
0x99 = 10011001
0x81 = 10000001
0x7E = 01111110

And if we take out all the off values we get…

 111111 
1      1
1 1  1 1
1      1
1 1111 1
1  11  1
1      1
 111111 

And now for the how too part of this random thought vomit.

//To set a bit in a byte on you do the following.
byte = byte | (1 << index);

//To set a bit in a byte off you do the following.
byte = byte & ~(1 << index);

//To read a bit form a byte you do the following.
boolean bitIsOn = ((byte >> index) & 1) == 1;

//Convert bytes to pixels
for(int y = 0; y < 8; y++) {
	byte smileData = smile[y];
	for(int x = 0; x < 8; x++) {
		if(((smileData >> x) & 1) == 1) {
			image.setPixel(x, y, 0xFFFFFFFF);
		}
	}
}

This is just one of the little tings I have been doing to get an efficient engine for Windows 95 and Java 1.1

EDIT:

I almost forgot the ASCII part of all this so I am using an 128x128 bitmap font that contains 256 characters. Each character is 8x8 so there are 16x16 characters in the image. I am storing all these characters in a single dimensional array like described above, and I can draw text to the screen like this.

public void drawASCII(String string, int x, int y, int color) {
	for(int index = 0; index < string.length(); index++) {
		char c = string.charAt(index);
		drawASCII(c, offset, y, color);
		x += 8;
	}
}

public void drawASCII(char c, int x, int y, int color) {
	int minX = Math.max(0, x);
	int maxX = Math.min(x + 8, width);
	int minY = Math.max(0, y);
	int maxY = Math.min(y + 8, height);
	for(int yy = minY; yy < maxY; yy++) {
		for(int xx = minX; xx < maxX; xx++) {
			if(Font.hasData(c, xx - x, yy - y)) {
				drawPixel(xx, yy, color);
			}
		}
	}
}

Comments

Nopykon 8 years, 5 months ago

Oooh! Oooh! Me too!

I made this originally for js13k. :) It's a 5x5 font, capital A-Z only.

/*

	TEXT OUT
	
*/
//each value is a 5x5 character, (5x5=25, fits inside a single u32)
var f5=[
	0x118fe3f,0x1f8fd2f,0x1f8863f,0xF8C62F,0x1f0fc3f,0x10fc3f,0x1f8f43f,0x118fe31,
	0x1f2109f,0x1f8c21f,0x1d2fe31,0x1f08421,0x15ad6bf,0x1dad6b7,0x1f8c63f,1113663,
	0x1fad63f,0x1d2fe3f,0x1f87c3f,4329631,0x1f8c631,0xe56e31,0x1fad6b5,0x11dbb71,
	0x1f87e31,0x1F1111F,-1
];
function chc(str,i){return str.charCodeAt(i)-'A'.charCodeAt();}
//draw single character
function k_font(C,dx,dy,c,inv){	
	for(var i=25;i--;)
		buf[(dy+(0|i/5))*SW+dx+i%5]|=(1&(f5[C]>>i)^inv)*c;
}   
//draw string
function k_str(str,x,y,c,inv,spc){
	spc=spc?spc:6;
	for(var i=str.length;i--;)k_font(chc(str,i),x+i*spc,y,c,inv);
}

Nice blog. :)

SkidRunner 8 years, 5 months ago

I was originally going to just use a long but there is no way of doing this in Java because I would need to use an unsigned long because there is no way to bit shift to the upper bytes in a long, I could use Bignum but this would give me object overhead and be a huge pain to work with. I am probably going to stop development in Java 1.1 because i seriously doubt that anyone is going to create a VM for Windows95 or old version of Linux just to test out my game.

and thank you!