The WeakReference class, monitoring memory leak and garbage collection in a Java application

Image
 Below is a Stack implementation that uses an internal resizeable array structure.  public class MyStack< T > implements Stack< T > { private static final int CAPACITY = 100 ; private Object[] array ; private int pos = 0 ; public MyStack () { this . array = new Object[ CAPACITY ] ; } @Override public void push ( T item) { if ( pos >= array . length / 2 ) { Object[] newArray = new Object[ pos * 2 ] ; System. arraycopy ( array , 0 , newArray , 0 , array . length ) ; array = newArray ; } array [ pos ++] = item ; } @Override public T pop () { if (isEmpty()) { throw new RuntimeException( "empty stack" ) ; } @SuppressWarnings ( "unchecked" ) T item = ( T ) array [ pos - 1 ] ; pos -= 1 ; return item ; } @Override @SuppressWarnings ( "unchecked" ) public T peek...

Notes on Bit Manipulation

Finding nth bit of an integer:

If you want to get the nth bit of a number, use this mask:

1 << n

So if you left shift 1 n times, you get the mask.

Below is the expression to find the nth bit:

number & (1 << n) 

Setting the nth bit to 1:

number | (1 << n)

Clearing the nth bit (setting it to zero):

We want to apply & operator with 0 to the nth bit. The mask we need is:

~(1 << n)

So the expression to clear the nth bit is:

number & ~(1 << n)

Flipping the nth bit:

We want to apply XOR with 1. Below is the expression:

number ^ (1 << n)


Finding if a bit in a position is set:

One way to do that is to right shift the number n times. Than the nth bit is the first bit. The following expression is the answer:

(number >> n) & 1


Finding number of 1 bits in unsigned integer

Loop through the bits and check. (Look at hammingWeight() method code below)

Finding Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. (Look at hammingDistance() method below)

https://www.youtube.com/watch?v=7jkIUgLC29I

TODO:

  • Modify Bit
  • Write a func that counts the number of bits different between two numbers

Summary:

public class Main {

    static int getNthBit(int value, int n) {


        int mask = 1 << n;


        return (value & mask) == 0 ? 0 : 1;


    }


    static int setNthBit(int value, int n) {


        int mask = 1 << n;


        return (value | mask);

    }

    static int clearNthBit(int value, int n) {


        int mask = ~(1 << n);


        return (value & mask);

    }

    static int flipNthBit(int value, int n) {


        int mask = (1 << n);


        return (value ^ mask);


    }

    static boolean isBitSet(int value, int n) {


        int mask = 1 << n;


        return (value & mask) != 0;

    }

    static boolean isBitSet2(int value, int n) {


        int shifted = value >> n;


        return (shifted & 1) != 0;


    }

    public static int hammingWeight(int n) {


        int count = 0;


        for (int i = 0; i < 32; ++i) {


            if ((n & (1 << i)) != 0) {

                ++count;
            }

        }


        return count;


    }

    public static int hammingDistance(int x, int y) {


        int d = 0;


        for (int i = 0; i < 32; ++i) {


            if ((x & (1 << i)) != ((y & (1 << i)))) {

                ++d;
            }

        }


        return d;


    }


    public static void main(String[] args) {


        int x = 5;


        System.out.println(getNthBit(x, 1));


        System.out.println(setNthBit(x, 1));


        System.out.println(clearNthBit(x, 1));

    }
}






Comments

Popular posts from this blog

My Crappy Looking Solution to "Binary Tree Common Ancestor" Problem

Trie Data Structure and Finding Patterns in a Collection of Words

A Graph Application in Java: Using WordNet to Find Outcast Words