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.)

Saturday, August 4, 2012

Revolution against corruption gone down the drain

Before I start writing anything,I understand that easiest thing in this world is to give advice and not following it.But still I would like to share my thoughts and also put forward the fact that most people in my country including me are not prepared to sacrifice their life for country.Here is what Bhagat Singh said

I emphasize that I am full of ambition and hope and of full charm of life. But I can renounce all at the time of need, and that is the real sacrifice. These things can never be hinderance in the way of man, provided he be a man. You will have the practical proof in the near future.  
- Selected writings of Shaheed Bhagat Singh (1986), Page. 65

We have already very many such leaders who spare some evening hours for delivering speeches. They are useless. We require ? to use the term so dear to Lenin ? the “professional revolutionaries”. The whole-time workers who have no other ambitions or life-work except the revolution.
  -Bhagat Singh.

What I saw at Jantar Mantar really made me sad.When revolution against corruption went weak,leaders opted for easy way out-political solution.If political solution would have been possible ,it would have happened in last 65 years.So when revolution demanded blood , sacrifice of life, our so called civil society leaders shied away from it.Had one of leaders given his life for war against corruption, by not eating,it would have caused the whole revolution turn into storm unstoppable by any government.It was like thresh hold, one sacrifice and all the sleeping public would have come to streets.Even in the worst case, public would not have come, but atleast it will be the true end to the war against corruption.Just because government is not listening or mass support is not there, leaders cannot abandon the cause.Had Mahatma Gandhi or Bhagat Singh thought about this, they would have never been able to do what they did.They brought revolutions.One sacrifice of Bhagat Singh made thousand more.This shying away from giving sacrifice has done more harm than good. People next time will not be willing to believe any such war against corruption.Revolutions need sacrifice and if it means giving your life for the sake of it,then be it or never start revolutions.

Now lot of people can ask one question:Why wont you do the same? I strongly believe that if I or anybody is leading the revolution, then leader cannot leave it before fulfilling the true cause of revolution.In this case, the revolution was to make the government pass the strong Lokpal Bill.Until that was fulfilled, they should not have back tracked.If god forbids,I am in same situation from where I started some revolution, I would not shy away from supreme sacrifice.But alas .....we do not have more Bhagat Singhs ....or Karatr Singh Sarabha or Bankim Chaterji or Ram Prasad Bismil.I would just end this blog with few lines from my favourite poem.

सरफ़रोशी की तमन्ना अब हमारे दिल में है,
देखना है जोर कितना बाजु-ए-कातिल में है ।

खींच कर लाई है सबको कत्ल होने की उम्मींद,
आशिकों का आज जमघट कूंच-ए-कातिल में है ।

सरफ़रोशी की तमन्ना अब हमारे दिल में है,
देखना है जोर कितना बाजु-ए-कातिल में है ।

है लिये हथियार दुश्मन ताक में बैठा उधर
और हम तैय्यार हैं सीना लिये अपना इधर

खून से खेलेंगे होली गर वतन मुश्किल में है
सरफरोशी की तमन्ना अब हमारे दिल में है


As long as we do not sacrifice our life for country ..........we will be sucked by some problem or the other.People have to be brought out from slumber and that needs sacrifice. Jai Hind









Thursday, July 5, 2012

Coomon sense and honesty at temples

I went to have darshan at shirdi.I think Shirdi Trust has done wonderful job in having the railings and other arrangements so that people do darshans without much hassle.They have put barricades at all the places so that people come in queue.They have arrangements for people to have water/tea while they are standing in queue.But actual reality is quite different. Darshans of SAI BABA is not hassle free but world war -III. People have made two rows/three rows where only one row was possible.There was absolutely no need to make second/third row.Some  people did not hesitate to jump over the 5 foot railing.On confrontation ,answer was "INDIA MIEN AISA HI HOTA HAI".(In india,its ok to break rules). Same has been followed by educated and un-educated alike .There was total chaos because people pushed each other.I could see few elderly people jumping over the railings as well.It was really bad state and lot of pushing happened but some how i am not able to blame the arrangements .Arrangements were fine if only people could have behaved little patiently.There was absolutely no need to make 2 rows where only 1 was intended.There was no need to jump the railings .There was no need to push each other.Only question intriguing me is this "Can chaos be prevented in places where thousands of people visit for darshans". Does the prevention of chaos only responsibility of the Trusts managing the temples/places of worship.Some where its the failure of our education system if chaos happen in-spite of good arrangements.We as society/people have accepted that in India only way forward is by pushing each other.If educated people take pride in breaking rules,what about the un-educated ones.I think we seriously need to revisit the education system and values imparted to students.The degradation of moral values is definitely a problem if only we as people see it and correct it.