Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

Operators and Assignments - Binary/Octal/Hex and Decimal Number Systems

Probably not directly required on exam but helpful when using bitwise and logical operators.

Decimal system

  • the decimal number system we use every day is built on base ten 1010
  • it is based on 10 positions numbered 0 thru 9
  • each position corresponds to a power of 10
    1024 = 1 x 103  -> 1 x 1000  = 1000
           0 x 102  -> 0 x  100  =  000
           2 x 101  -> 2 x   10  =   20
           4 x 100  -> 4 x    1  =    4 
                                   ----
                                   1024

Binary system

  • computer memory is based on the electrical representation of data
  • each memory position is represented by a bit which can be either 'on' or 'off'. This makes it easier to represent computer memory using a base 2 number system rather than the base 10 decimal system.
  • the binary system represents numbers by a series of 1's and 0's which correspond to 'on' and 'off' values
  • a 1 represents an 'on' position, a 0, an 'off' position
  • a byte is represented by 8 bits numbered 0 to 7 from left to right
  • the leftmost bit is called the high-order bit, the right most bit, the low-order bit
  • in the decimal system, each position corresponds to a power of 10, in the binary system, each position corresponds to a power of 2
01001001 = 0 x 27    -> 0 x 128  =  0
           1 x 26    -> 1 x  64  = 64
           0 x 25    -> 0 x  32  =  0
           0 x 24    -> 0 x  16  =  0
           1 x 23    -> 1 x   8  =  8
           0 x 22    -> 0 x   4  =  0
           0 x 21    -> 0 x   2  =  0
           1 x 20    -> 1 x   1  =  1
                                   --
                                   73
  • the largest number which can be represented by a byte is 255 or
    128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 or the bit pattern: 1111 1111
  • the smallest number is 0 represented by the bit pattern: 0000 0000
  • 0 to 255 gives 256 possible values

Two's-complement

  • the two's complement method allows us to represent negative and positive values within the 0 to 256 bit positions
  • in this system the numbers 0 thru 127 represent themselves and the numbers 128 to 256 represent negative numbers where 255 = -1, 254 = -2, 253 = -3, ...
  • -1 is represented by 256 - 1 = 255, -127 is represented by 256-127 = 129, and -50 would be represented by 256 - 50 = 206
  • the high-order bit (the 7th position) is reserved for the sign value of a number
  • a 0 in the high order bit means 'the sign value is set to positive'
  • a 1 in the high-order bit means 'the sign value is set to negative'
01111111 =   0 +  64 + 32 + 16 + 8 + 4 + 2 + 1  =   127
10000000 = 128 or set sign negative
11111111 =   0 -  64 - 32 - 16 - 8 - 4 - 2 - 1  =  -127  
  • larger numbers are represented by increasing the number of bits in a memory block
  • this is done in multiples of 8 hence 16-bit, 32-bit and 64-bit memory
  • 16-bit memory can store numbers up to 216-1, 32-bit, 232-1, 64-bit, 264-1
  • if signed numbers are being used, the left-most bit still represents the sign
  • so, 16-bits allows us to store 0 to 65,535 positions (216 - 1), 32-bits, 0 to 4,294,967,295 (232 - 1)
  • using two's-complement arithmetic with 32-bit memory, subtract the negative number from 65,536 to find it's positive complement ie -336 would be represented by 65536 - 336 = 65200

Octal system

  • uses base 8
  • octal digits are represented by 0 thru 7
  • each position is a power of 8
  • each octal number can be represented by 3 binary digits
  • 22+21+20 = 4+2+1 = 7
Decimal Octal Binary
0 0 000
1 1 001
2 2 010
3 3 011
4 4 100
5 5 101
6 6 110
7 7 111
  • to convert from Octal to Binary just replace the octal digit with the corresponding binary pattern
        Octal: 17       Binary: 001 111
    
  • to convert from Binary to Octal just replace the binary pattern with the corresponding octal digit
        Binary: 111 010 Octal:  72
    

Hexidecimal system

  • the hexidecimal system uses a base of 16
  • hexidecimal digits are represented by 0 thru 9 and the letters A,B,C,D,E,F
  • one hexidecimal digit corresponds to a four-digit binary number
  • 23+22+21+20 = 8 + 4 + 2 + 1 = 15
Decimal Hex Binary Decimal Hex Binary
0 0 0000 8 8 1000
1 1 0001 9 9 1001
2 2 0010 10 A 1010
3 3 0011 11 B 1011
4 4 0100 12 C 1100
5 5 0101 13 D 1101
6 6 0110 14 E 1110
7 7 0111 15 F 1111
  • this makes it easy to convert a number from binary to hex (just replace the binary pattern with the hex digits) or from hex to binary (replace the hex digit with the binary pattern)
Binary: 0000 1111                -> Hex:     0x0F
Binary: 1011 0011 0000 0010      -> Hex:     0xB302

Hex:    0xA0FF                   -> Binary:  1010 0000 1111 1111
Hex:    0xF075                   -> Binary:  1111 0000 0111 0101

Converting between number systems

  • to convert a decimal number to a Hex, Octal or Binary number divide by the required base, the resulting remainders, in reverse order represent the required value
Convert Decimal 49 to Binary 49:

    49 / 2 = 24      remainder: 1      ( 49 - 2*24 = 49 - 48 = 1)
    24 / 2 = 12      remainder: 0      ( 24 - 2*12 = 24 - 24 = 0)
    12 / 2 =  6      remainder: 0      ( 12 - 2* 6 = 12 - 12 = 0)
     6 / 2 =  3      remainder: 0      (  6 - 2* 3 =  6 -  6 = 0)
     3 / 2 =  1      remainder: 1      (  3 - 2* 1 =  3 -  2 = 1)
     1 / 2 =  0      remainder: 1      (  1 - 2* 0 =  1 -  0 = 1)
 
 Proof: 110001 = 32+16+0+0+0+0+1 = 4910
 
Convert Decimal 49 to Octal 49:

    49 / 8 = 6      remainder:  1    ( 49 - 8*6 = 49 - 48 = 1)
     6 / 8 = 0      remainder:  6    (  6 - 8*0 =  6 -  0 = 6)
     
 Proof: 618 = (6 * 81) + (1 * 80) = 48 + 1 = 4910     
 
Convert Decimal 49 to Hexidecimal 49:

    49 / 16 = 3     remainder:  1     ( 49 - 16*3 = 49 - 48 = 1)
     3 / 16 = 0     remainder:  3     (  3 - 16*0 =  3 -  0 = 3) 
     
 Proof: 3116 = (3*161) + (1*160) = 48 + 1 = 4910
  • in Java octal numbers are represented by 3 digits beginning with a zero, so 618 would be written as 061
  • Hex numbers are always represented by 4 digits preceeded by 0x, so 3116 would be written as 0x0031
  • when converting large decimal numbers to binary, the simplest method is to convert to Hex and then to binary
Convert 4823 to Hex:
    
4823 / 16 = 301 remainder:  7  (4823 - 16*301 = 4823 - 4816 =  7)
 301 / 16 =  18 remainder: 13  ( 301 - 16* 18 =  301 -  288 = 13)
  18 / 16 =   1 remainder:  2  (  18 - 16*  1 =   18 -   16 =  2)
   1 / 16 =   0 remainder:  1  (   1 - 16*  0 =    1 -    0 =  1)
       
 Hex value: 0x12D7 
 Hex value converted to binary: 0001 0010 1101 0111        

  • when converting large binary numbers, the simplest method is to convert to Hex and then to decimal
Convert Binary 0001 0010 1101 0111 to decimal

    0001    1   1 * 163 = 4096
    0010    2   2 * 162 =  512
    1101    D  13 * 161 =  208
    0111    7   7 * 160 =    7
                          ----
                          4823

Study aids

  • If you have Windows 95 you can use the Calculator in the Scientific mode (Start->Programs->Accessories->Calculator) to check results of decimal to hex, binary, and octal conversions.
  • You can also use the Java Integer wrapper class to output binary, hex and octal strings. Example: System.out.println(Integer.toBinaryString(-29));
  • Marcus Greene has a great applet that lets you play around with bit-shifting at http://www.software.u-net.com/applets/BitShift/BitShiftAr.html

References:



Conversions Promotion Overflow Unary Prefix Arithmetic
  Bin/Hex/Octal Bitwise Shift Comparison Logical Assignment
  Cast Ternary String equals() Precedence Bit vs Logic
  Method Invocation