We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Access single Bit within BitSet (Nibble, Byte,...)
Page Index Toggle Pages: 1
Access single Bit within BitSet (Nibble, Byte,...) (Read 622 times)
Access single Bit within BitSet (Nibble, Byte,...)
Sep 22nd, 2008, 2:36am
 
I am currently working on a project where I work bitwise. I need to get the single bits which make up a character.

Generally it would be nice if Processing offered a way to work with single bits within multiple bits (bitsets) like nibbles, bytes, etc... depending on what datatypes you are working with (characters, colors, arbitrary BitSets,...). Or to tell me if something like this already exists within Processing (or the underlying Java) and how to work with it.

I would like that objects of nearly all types (where technically possible) have a method like object.bitAt(int bitPointer, String bitEndianness) which returns the flag (=boolean) of the bit at the position where bitPointer points to, where the optional argument bitEndianness determines in which direction the object's bits are being read. A similar method should be available for writing.

My (resource inefficient) workaround code meanwhile, note function bitAt():

Quote:
// Basic Setup

void setup() {
  size(100,100);
  noLoop();
}

// Draw Setup

void draw() {
  charDump(str(char1),'u'); // UnicodeChar is dumped to console as 16 bit. Strangely the more significant bits are written on the left side through the binary() function. But in western cultures we read from left-to-right and count from smaller to larger. Hence I would expect that the least significant bit is written on the left (=the beginning) of the string. Processing (or is it the underlying Java?) has a inconsistency here!!!
  bitAtLoop(char1, "up", 0, 15); // The "up" mode matches the outcome of the binary() function.
  println("---");
  charDump(str(char1),'u');
  bitAtLoop(char1, "down", 0, 15); // The "down" mode doesn't match the outcome of the binary() function.
  println("---");
  charDump(str(char1),'b'); // UnicodeChar is first cut to 8 bit. Then dumped to console.
  bitAtLoop(char1, "up", 8, 15); // The mode "up" and the limiters from 8 to 15 math the outcome of the binary() function.
}

// Variable Setup

String str1 = "Some Characters";
char char1 = 'X';
int index1 = 7;

// Function Setup

boolean bitAt(char c, int pointer) {
  String b = binary(c);
  if (b.charAt(pointer) == '1') {
    return true;
  }
  else if (b.charAt(pointer) == '0') {
    return false;
  }
  else {
    return false;
  }
}

void bitAtLoop(char c, String direction, int low, int high) {
  if (direction == "up") {
    for (int i = low; i <= high; i++) {
      println(bitAt(c , i));
    }
  }
  if (direction == "down") {
    for (int i = high; i >= low; i--) {
      println(bitAt(c , i));
    }
  }
}

void charDump(String s, char mode) {
  switch (mode) {
  case 'b': // Byte 8-Bit
    for (int i = 0 ; i <= s.length()-1 ; i++ ) {
      println(s.charAt(i) + " " + binary(byte(s.charAt(i))) + " 0x" + hex(byte(s.charAt(i))));
    }
    break;
  case 'u': // Unicode 16-Bit
    for (int i = 0 ; i <= s.length()-1 ; i++ ) {
      println(s.charAt(i) + " " + binary(s.charAt(i)) + " 0x" + hex(s.charAt(i)));
    }
    break;
  }
}

Re: Access single Bit within BitSet (Nibble, Byte,
Reply #1 - Sep 22nd, 2008, 3:47pm
 
you can use the bitwise operators to check individual bits

http://processing.org/reference/bitwiseAND.html
http://processing.org/reference/leftshift.html

so your bitAt becomes:

boolean bitAt(char c, int pointer) {
 return ((c & (1 << pointer)) != 0);
}

false if bit = 0, true if 1. pointer is 0 for leftmost bit.

set bits using:

c |= (1 << pointer);

clear bits using:

c &= ~(1 << pointer)

some background here:
http://en.wikipedia.org/wiki/Bit_manipulation

(i dunno, kids these days. wasn't like this when we had to program in assembler... 8))

and whilst we're here:

if (direction == "down")

is wrong - use direction.equals("down")
Re: Access single Bit within BitSet (Nibble, Byte,
Reply #2 - Sep 22nd, 2008, 9:40pm
 
It's not documented in the Processing reference library but in addition to the << (left shift) , >> (right shift), | (Bitwise OR), and & (Bitwise AND) you can also use ^ (bitwise exclusive or - XOR).  I've never tried it in Processing but there should also be a 1's complement which is usually ~ .

If you're not sure what an XOR does it should be on Wikipedia.
Re: Access single Bit within BitSet (Nibble, Byte,
Reply #3 - Sep 29th, 2008, 4:16pm
 
koogs & azkennedy!

Thanks for your suggestions!

Without really understanding the Bit Manipulation Logics, I implemented koog's BitAt() function, and hope that this has efficiency effects under the hood.

Stefan
Page Index Toggle Pages: 1