Archive

Archive for July, 2013

reverse bits in a byte

July 25th, 2013 No comments

Working on a code for an arduino project I had the problem that the letters i had created for my 8×8 LED matrix were all mirrored. A letter looks like this:

byte letter_A[8] = { 0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00};

Now the rows are in the correct order (letter not upside down), but the leds of one row are wrong meaning the LSB should be the MSB. I found 3 solutions on the net which are all 3 cool in their own way:

The first version is a 1 to 1 translation of how i would do it on paper. Take the most left and swap it with the most right. Then take the second and swap it with the second last and so on …

// "00110011" becomes "11001100"
byte byteShift(byte num) {
  byte var = 0;     
  int i, x, y, p;
  int s = 8;    // number of bits in 'num'.
 
  for (i = 0; i < (s / 2); i++) {
    // extract bit on the left, from MSB
    p = s - i - 1;
    x = num & (1 << p);
    x = x >> p;  
    // extract bit on the right, from LSB
    y = num & (1 < < i);
    y = y >> i;
 
    var = var | (x < < i);       // apply x
    var = var | (y << p);       // apply y
  }
  return var;
}

The next version uses bit masks for reversing the order of bits

// Reverse the order of bits in a byte. 
// I.e. MSB is swapped with LSB, etc. 
byte Bit_Reverse( byte x ) 
{ 
    //          01010101  |         10101010
    x = ((x >> 1) & 0x55) | ((x < < 1) & 0xaa);
    //          00110011  |         11001100 
    x = ((x >> 2) & 0x33) | ((x < < 2) & 0xcc);
    //          00001111  |         11110000 
    x = ((x >> 4) & 0x0f) | ((x < < 4) & 0xf0); 
    return x;    
}

The last one is not pretty but pretty fast 😉

// 36 bytes small, inelegant assembler, but faster 
byte bitswap (byte x)
{
  byte result;
 
    asm("mov __tmp_reg__, %[in] \n\t"
      "lsl __tmp_reg__  \n\t"   /* shift out high bit to carry */
      "ror %[out] \n\t"  /* rotate carry __tmp_reg__to low bit (eventually) */
      "lsl __tmp_reg__  \n\t"   /* 2 */
      "ror %[out] \n\t"
      "lsl __tmp_reg__  \n\t"   /* 3 */
      "ror %[out] \n\t"
      "lsl __tmp_reg__  \n\t"   /* 4 */
      "ror %[out] \n\t"
 
      "lsl __tmp_reg__  \n\t"   /* 5 */
      "ror %[out] \n\t"
      "lsl __tmp_reg__  \n\t"   /* 6 */
      "ror %[out] \n\t"
      "lsl __tmp_reg__  \n\t"   /* 7 */
      "ror %[out] \n\t"
      "lsl __tmp_reg__  \n\t"   /* 8 */
      "ror %[out] \n\t"
      : [out] "=r" (result) : [in] "r" (x));
      return(result);
}

Categories: arduino Tags:

hope and other stuff

July 7th, 2013 No comments

There is an enlightening article on why maybe – only maybe – not all hope is lost in the long run. While the western world is shaken by the latest leaks and what followed in its wake, there is this story about true DIY hackerism in one of the most improbable places in the world. Here comes a positively strange (but seemingly true) story about Afghanistan:

http://www.psmag.com/culture/the-merry-pranksters-who-hacked-the-afghan-war-60873/

If you’d like to know how Dr Dave Warner looks like: https://www.youtube.com/watch?v=nGdk9JwZWzY

Categories: Uncategorized Tags:

Who has your back??

July 6th, 2013 No comments

The the Electronic Frontier Foundation knows – here:

https://www.eff.org/who-has-your-back-2013

Nice overview of which company/website is really trying to protect your data. I suddenly feel the urge to twitter more and blog less as WordPress doesn’t look to good but Twitter does.

Categories: political, software Tags:

Keine Coke mit E. J. Snowden

July 3rd, 2013 1 comment

Coca Cola macht gerade mächtig Werbung, man kann eine Flasche mit seinem eigenen Label bestellen. Da steht dann sowas drauf wie: “Ich trink meine Coke mit XYZ”. Dazu gibts auch ne nette Webseite: http://www.coke.de/peter

Nun schaut mal, was passiert, wenn man dort in die Flasche “Edward Snowden” eintragen will 🙂 FNORD! Übrigens “Osama Bin Ladin” oder “Barack Obama” funktioniert.

Categories: wtf Tags: ,