public class MatDasar {
public static void main(String args[]){
//Aritmatik menggunakan interger
System.out.println("Aritmatik Inteerger Arithmetic");
int a = 1+1;
int b = a*3;
int c = b/4;
int d = c-a;
int e = -d;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
//Aritmatik menggunakan double
System.out.println("\nAritmatik Titik Menggambang");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
public class Modulus {
public static void main(String args[]){
int x = 42;
double y = 42.25;
System.out.println("x mod 10 =
" + x % 10);
System.out.println("y mod 10 =
" + y % 10);
}
}
public class OpGabungan {
public static void main(String args[]){
int a = 1;
int b = 2;
int c = 3;
a += 5;
b *= 4;
c += a * b;
c %= 6;
System.out.println("a = " +
a);
System.out.println("b = " +
b);
System.out.println("c = " +
c);
}
}
public class OpInk {
public static void main(String args[]){
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
System.out.println("a = " +
a);
System.out.println("b = " +
b);
System.out.println("c = " +
c);
System.out.println("d = " +
d);
}
}
public class OpInkDek {
public static void main(String args[]){
char c = 'R';
byte j = 127;
short k = 32767;
System.out.println("c = " +
c);
++c;
System.out.println("c = " +
c);
++c;
System.out.println("c = " +
c);
System.out.println("j = " +
j);
--j;
System.out.println("j = " +
j);
++j;
System.out.println("j = " +
j);
System.out.println("k = " +
k);
k -= 4;
System.out.println("k = " +
k);
k +=5;
System.out.println("k = " +
k);
}
}
public class LogikaBit {
public static void main(String args[]){
String biner [] = {
"0000", "0001", "0010", "0011",
"0100", "0101","0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"
};
int a = 3; //0 + 2 + 1 atau 0011 dalam biner
int b = 6; //4 + 2 + 0 atau 0110 dalam biner
int c = a | b;
int d = a & b;
int e = a ^ b;
int f = (~a & b) | (a & ~b);
int g = ~a & 0x0f;
System.out.println("a = " + biner[a]);
System.out.println("b = " + biner[b]);
System.out.println("a|b = " + biner[c]);
System.out.println("a&b = " + biner[d]);
System.out.println("a^b = " + biner[e]);
System.out.println("~a&b|a&~b = " + biner[f]);
System.out.println("~a = " + biner[g]);
}
}
public class GeserKiri {
public static void main(String args[]){
byte a = 64, b;
int i;
i = a << 2;
b = (byte) (a << 2);
System.out.println("Nilai awal dari a: " + a);
System.out.println("i danb: " + i + " " + b);
}
}
public class PerkalianDua {
public static void main(String args[]){
int i;
int angka = 0xFFFFFFE;
for(i=0; i<4; i++){
angka = angka << 1;
System.out.println(angka);
}
}
}
public class PenggeseranKanan {
public static void main(String args[]){
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xfl;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] +
hex[b & 0x0f]);
}
}
public class PenggeseranUnsigned {
static public void main(String args[]){
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xfl;
byte c = (byte) (b >> 4);
byte d = (byte) (b >>> 4);
byte e = (byte) ((b & 0xff) >> 4);
System.out.println(" b = 0x" + hex[(b >> 4) & 0x0f]
+ hex[b & 0x0f]);
System.out.println(" b >> 4 = 0x" + hex[(c >> 4)
& 0x0f] + hex[c & 0x0f]);
System.out.println("b >>> 4 = 0x" + hex[(d >>
4) & 0x0f] + hex[d & 0x0f]);
System.out.println("(b & 0xff) >> 4 = 0x" + hex[(e
>> 4) & 0x0f] + hex[e & 0x0f]);
}
}
public class PenugasanBitwise {
public static void main(String args[]){
int a = 1;
int b = 2;
int c = 3;
a |= 4;
b >>= 1;
c <<= 1;
a ^= c;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
public class LogikaBool {
public static void main(String args[]){
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("a|b = " + c);
System.out.println("a&b = " + d);
System.out.println("a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println("!a = " + g);
}
}