Monday, July 21, 2014

Monday, January 21, 2013

Sunday, December 30, 2012

My code is in github now

https://github.com/mikedasuya/ds

Security
http://www.schneier.com/blog/

Applied Cryptography
Bruce Schneier
John Wiley & Sons, 1996
ISBN 0-471-11709-9
Paperback - 784 pages - $60.00
ISBN 0-471-12845-7
Hardcover - 784 pages - $85.00

Tuesday, December 11, 2012

C stricks-ricthie

if there ispointer airthmetic following things must be consindered.
1)Pointer cant point to register variable.

2)Pointer subtraction must be stored in special variable ptrdiff_t and not in integer because it can overflow the integer.

               In a two's complement number system, x &= (x-1) deletes the rightmost 1-bit
in x. Explain why. Use this observation to write a faster version of bitcount.

Denis ricthie

Monday, December 3, 2012

Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.



                Write a function setbits(x,p,n,y) that returns x with the n bits that begin at
position p set to the rightmost n bits of y, leaving the other bits unchanged.

So there is x = 32 (0010000)
Y=19 (0010011)

Now p =4
and n =2

So right most 2 bits of y are 11.
So we move these two bits to X position (5,6).

After this code X=48(0110000)


#include
#include


void countNumberOfbits();
int main() {
countNumberOfbits();
return 0;
}

void countNumberOfbits() {
char x = 32;
char y = 19;
int p =4;
int n = 2;

y = y & (~0 << 4);
y = y & (~0 >> 2);
x = x|y;



printf("\n Count is ---:%d: --\n",x);fflush(stdout);
}


Sunday, December 2, 2012

Making tree from array using stack and doing left first traversal



#include
#include
#include "stack.h"

char* replace(char, char*);
void countNumberOfbits();
void constructHeap(int ar[]);

void fillPrimary(stack*,stack*);
void fillSecondry(stack*,stack*);
void traversal(Node * root);


int main() {
/*
    char * ptr = (char*)malloc(6);
    ptr[0] = 'a';
    ptr[1] = 'b';
    ptr[2] = 'c';
    ptr[3] = 'c';
    ptr[4] = 'e';
    ptr[5] = '\0';
    char * mainString = "itsasit is cb";

    replace('c', ptr);
*/
    int ar[10] = {0,1,2,3,4,5,6,7,8,9};
    constructHeap(ar);
//    countNumberOfbits(;
    return 0;
}

void constructHeap(int ar[]) {
    struct Node * root = NULL;
    struct Node * ptr = NULL;
    struct Node * mvptr = NULL;
    stack * st_primary = new stack();
    stack * st_secondry = new stack();
   
    for (int i =0; i< 10; i++) {
        ptr = new Node();
        ptr->lfptr = NULL;
        ptr->rptr = NULL;
        ptr->data = ar[i];
        if (st_primary->length() == 0) {
            printf("\n -------tree is here to stay ---\n");fflush(stdout);
            st_primary->push(ptr);
            root = ptr;
        } else {
            mvptr = st_primary->pop();
            if (mvptr->lfptr != NULL
                   && mvptr->rptr != NULL) {
                mvptr = st_primary->pop();
            }
            fillSecondry(st_primary,st_secondry);
            if (mvptr->lfptr == NULL) {
               
                mvptr->lfptr = ptr;
                st_secondry->push(ptr);
                fillPrimary(st_primary,st_secondry);
                st_primary->push(mvptr);
   
            } else if (mvptr->rptr == NULL) {
                mvptr->rptr = ptr;
                st_secondry->push(ptr);
                fillPrimary (st_primary, st_secondry);
                st_primary->push(mvptr);
            }
        }
    }   
    traversal(root);
   
}


void fillPrimary(stack * st_primary,stack * st_secondry) {
    int len = st_secondry->length();
    for (int i = 0; i < len;i++) {
        printf("\n -----fill primary --:%d:--\n",i);fflush(stdout);
        st_primary->push(st_secondry->pop());
    }

}

void fillSecondry(stack * st_primary, stack * st_secondry) {
    int len = st_primary->length();
    for (int i = 0; i < len;i++) {
        st_secondry->push(st_primary->pop());
    }
}

void traversal(Node * root) {
    Node * mvptr = NULL;
    stack* st_traverse = new stack();
    st_traverse->push(root);
        while (st_traverse->length() != 0) {
        mvptr = st_traverse->pop();
        if (mvptr->rptr) {
            st_traverse->push(mvptr->rptr);
        }
        printf("\n ---Traversla :%d: --\n",mvptr->data);fflush(stdout);
        while (mvptr->lfptr) {
            mvptr = mvptr->lfptr;
            printf("\n Traversal 1---:%d: ---\n",mvptr->data);fflush(stdout);
            if (mvptr->rptr) {
                st_traverse->push(mvptr->rptr);
            }
       
        }
       
    }

}



/*void getNode(node * root) {
    int level = 0;
    if (root->lfptr == NULL) {
        return root->leftPtr;
    } else if (root->rptr == NULL) {
        return root->rptr;
    } else if (root->lfptr != NULL) {
        level++;
        pushlevel(level);
       
   
    } else if (root->rptr != NULL) {
   
    }
   
}
*/


void countNumberOfbits() {
    char x = 23;
    int count = 0;
    printf("\n ---one is :%d: --\n",~(~0 << 1));
    for (int i = 0; i < 7;i++) {
        char y = (x >> i) & ~(~0 << 1);
        printf("\n ---:%d:--x is \n",x);fflush(stdout);
        if (y == 1) {
            count++;
            printf("\n ---x ==1 \n");fflush(stdout);
        }
    }
    printf("\n Count is ---:%d: --\n",count);fflush(stdout);
}

char* replace(char ch, char * ptr) {

    char * str = ptr;
    printf("\n ---STR :%s:--\n",str);fflush(stdout);
    char * ptr_f = str;
    int j = 0;
    for (int i=0;i < 6; i++) {
        if (str[i] != ch) {
            printf("\n ---i:%d:-j-:%d: \n",i, j);fflush(stdout);
            str[j] = str[i];
            j++;
        }

    }
    str[j] = '\0';
    printf("\n ---STR --:%s:\n",ptr);fflush(stdout);
    return str;
}


Thursday, November 29, 2012

New programming puzzles

             Write the function any(s1,s2), which returns the first location in a string s1
where any character from the string s2 occurs, or -1 if s1 contains no characters from s2.
(The standard library function strpbrk does the same job but returns a pointer to the
location.)