Wednesday, February 23, 2011

1s Complement and 2's Complement

In general, we (human beings) express negative numbers by placing a minus (-) sign at the left end of the number. Similarly while representing the integers in binary format, we can leave the left-most bit be the sign bit. If the left-most bit is a zero, the integer is positive; if it is a one, it is negative.
 
1's complement ---reverse all the bits
2's complement ---reverse all the bits + 1

Examples
Original number: 0111011
1's complement: 1000100
2's complement: 1000101

Original number: 1111111
1's complement: 0000000
2's complement: 0000001

Original number: 1111000
1's complement: 0000111
2's complement: 0001000 

Sunday, January 30, 2011

Tree Set In Java

TreeSet stores objects in a sorted manner. TreeSet stores its elements in a tree and they are automatically arranged in a sorted order.


TreeSet is not synchronized. If more than one thread wants to access it at the same time then it must be synchronized externally.

 import java.util.*;  
 public class DemoTreeSet {  
  public static void main(String[] args) {  
   TreeSet<String> t1 = new TreeSet<String>();  
   t1.add("zzz");  
   t1.add("aaa");  
   t1.add("ddd");  
   t1.add("bbb");  
   t1.add("nnn");  
   t1.add("lll");  
   System.out.println("Contents of treeset");  
   Iterator it1 =t1.iterator();  
   while(it1.hasNext()){  
    Object o1 = it1.next();  
    System.out.println(o1);  
   }  
  }  
 }  

MORE EXAMPLE

DBMS QUESTIONS AND ANSWERS