Posts Tagged ‘ Exmaple ’

Dividing an array

Even if you don’t care about the practical applications of it in modern programming, it offers a look back when programming was more of a pain in the ass because shit needed to be set up properly as well as having a good algorithm. Actually it is still relevant in deeper technical programming that is closer to the hardware level. Probably not so much for app developers.

So let’s say you have an array using 16-bit integers. So you would have something like this:

unsigned short *myArray = new unsigned short[1024];

So you fill it up with data and all is good right? Well now your next requirement is that you want to divide each element into two parts, the lower byte and upper byte because the upper byte indicates the flashing pattern of the red LED and the lower byte indicates the flashing pattern of the green LED. Well FML, right? No!

We make a new byte pointer (a char in this case) and make it point to the typecasted main array:

unsigned char* birchPointer;
birchPointer = (unsigned char*) myArray;

Now because a char is a byte in size, when you advance the index it will point to the next 8-bit position. So let’s say myArray contains the following information:

myArray[0] = 0xBEEF;
myArray[1] = 0xCAFE;
myArray[2] = 0xBABE;

Now if you use the char pointer to access myArray, you’ll get the following information (providing your system is big endian):

birchPointer[0] = 0xBE;
birchPointer[1] = 0xEF;
birchPointer[2] = 0xCA;
birchPointer[3] = 0xFE;
birchPointer[4] = 0xBA;
birchPointer[5] = 0xBE;

If you’re using a machine that’s little endian (which most PCs are), then the byte values will be swapped like so (due to little endian storing the lower byte first in the memory sequence):

birchPointer[0] = 0xEF;
birchPointer[1] = 0xBE;
birchPointer[2] = 0xFE;
birchPointer[3] = 0xCA;
birchPointer[4] = 0xBE;
birchPointer[5] = 0xBA;

So there you have it: splitting a 16-bit array into an 8-bit array and learning something about how endians affect it.

Read more:
Size of C++ Datatypes
Big endian vs. little endian

Implementing a thread in Android

Here is a simple way to implement a thread into your program.  I usually use it when I want to scan something every few seconds or something.  The onStart and onStop will start and kill your thread.  The sendEmptyMessage is what you use in the thread to update your UI.   All of these methods will go below your onCreate(Bundle whatever) function.

You don’t have to call anything in the onCreate function to make it run.  In this example it will start running when the program starts, however you could call startScan() where ever you want

public void onStart(){

public void onStop(){
    isRunning = false;

public void startScan() {
    new Thread() {
        public void run() {
            while (isRunning) {
                    Thread.sleep(2000); // How long the thread will wait in milliseconds.  Not 100% accurate

                    if(!isRunning) {
                    // Do stuff here
                    hRefresh.sendEmptyMessage(REFRESH SCREEN);
                }catch(Exception e){

// Refresh handler, necessary for updating the UI in a/the thread
Handler hRefresh = new Handler(){
    public void handleMessage(Message msg) {
        case REFRESH_SCREEN:
        //Do stuff in here that you want updated in the screen

Find this helpful? Know a better way of implementing a simple thread? Please feel free to share. Do you need this code uploaded into a sample project? If there is a demand I can start creating basic empty projects that illustrate the code in action. Enjoy!