This may be somewhat of an odd question as well as my first one ever on this site and a pretty complicated thing to ask basically I have this decompresser for a very specific archived file, I barely understand this but from what i can grasp its some sort of "bit mask" it reads the first 2 bytes out of target file, and stores them as a sequence.

The first for loop is where I get confused

Say for arguments sake mask is 2 bytes `10 04`, or 1040(decimal) thats what it usually is in these files

```
    for (t = 0; t<16; t++) {
        if (mask & (1 << (15 - t))) { 

```

This seems to be looping through all 16 bits of those 2 bytes and running an AND operation on mask (1040) on every bit?  

The if statement is what I don't understand completely? Whats triggering the `if`? If the bit is greater then 0? 

Because if mask is 1040, then really what were looking at is

```
if(1040 & 32768)    index 15
if(1040 & 16384)    index 14
if(1040 & 8192)     index 13
if(1040 & 4096)     index 12
if(1040 & 2048)     index 11
if(1040 & 1024)     index 10
if(1040 & 512)      and so on.....
if(1040 & 256)

```

I just really need to know whats triggering this if statement? i think i might be over thinking it, but is it simply trigger if the current bit is greater then 0?

The only other thing i can do is compile this source myself, insert printfs on key variables and go hand in hand with a hex editor and try and figure out whats actually going on here, if anyone could give me a hand would be awesome.

```
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

uint8_t  dest[1024 * 1024 * 4]; // holds the actual data

int main(int argc, char *argv[]) {
    FILE *fi, *fo;
    char fname[255];

    uint16_t mask, tmp, offset, length;
    uint16_t seq;
    uint32_t dptr, sptr;
    uint16_t l, ct;
    uint16_t t, s;
    int test_len;
    int t_length, t_off;

    // Print Usage if filename is missing
    if (argc<3) {
        printf("sld_unpack - Decompressor for .sld files ()\nsld_unpack <filename.sld> <filename.t2>\n");
        return(-1);
    }

    // Open .SLD-file
    if (!(fi = fopen(argv[1], "rb"))) {
        printf("Error opening %s\n", argv[1]);
        return(-1);
    }

    dptr = 0;

    fread((uint16_t*)&seq, 1, 2, fi); // read 1st 2 bytes in file
    test_len = ftell(fi);

    printf("[Main Header sequence: %d]\n 'offset' : %d \n", seq, test_len);
    sptr = 0;

    fread((uint16_t*)&seq, 1, 2, fi);

    while (!feof(fi)) { // while not at the end of the file set mask equal to sequence (first 2 bytes of header)
        mask = seq;
        // loop through 16 bit mask

        for (t = 0; t<16; t++) {
            if (mask & (1 << (15 - t))) { // check all bit fields and run AND check to if value greater then 0?

                test_len = ftell(fi);
                fread((uint16_t*)&seq, 1, 2, fi); // read 

                sptr = sptr + 2; // set from 0 to 2
                tmp = seq; // set tmp to sequence
                offset = ((uint32_t)tmp & 0x07ff) * 2; 

                length = ((tmp >> 11) & 0x1f) * 2; //  32 - 1?

                if (length>0) {

                    for (l = 0; l<length; l++) {
                        dest[dptr] = dest[dptr - offset];
                        dptr++;

                    }
                }
                else { // if length == 0 

                    t_length = ftell(fi);
                    fread((uint16_t*)&seq, 1, 2, fi);

                    sptr = sptr + 2;
                    length = seq * 2;

                    for (s = 0; s<length; s++) {
                        dest[dptr] = dest[dptr - offset];
                        dptr++;

                    }
                }
            }
            else { // if sequence AND returns 0 (or less)?

                fread((uint16_t*)&seq, 1, 2, fi);
                t_length = ftell(fi);

                sptr = sptr + 2;
                dest[dptr++] = seq & 0xff;
                dest[dptr++] = (seq >> 8) & 0xff;
            }
        }
        fread((uint16_t*)&seq, 1, 2, fi);

    }

    fclose(fi);

    sprintf(fname, "%s\0", argv[2]);

    if (!(fo = fopen(fname, "wb"))) { // if file 
        printf("Error creating %s\n", fname);
        return(-1);
    }

    fwrite((uint8_t*)&dest, 1, dptr, fo);

    fclose(fo);

    printf("Done.\n");

    return(0);
}

```