Hello world, bits operations
Getting started with Java programming
This is a starter code (Hello.java):
public class Hello {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Hello world!
Write a program, that displays your name.
Write a program, that displays a sum of two numbers. Try also: multiplication (*), substracting (-) and dividing (/).
Does dividing 11 per 2 give correct answer?
Find binary representation for numbers: 81, 65.
Calculate value of: 81&16, 65&16.
Check your Task 3 writing java code.
public class Hello {
public static void main(String[] args) {
// "0b" means "binary"
System.out.println(0b10101);
// some expressions
System.out.println(0b10101 & 10);
System.out.println(5 | 4);
}
}
Instead of writing the same value all the time, you can use a variable, i.e. byte.
Create a new byte variable. Next write program, that extracts powers of 2 (1,2,4,8,16,...) from the byte. i.e.: 81=64+0+16+0+0+0+1
public class Hello {
public static void main(String[] args) {
// create a byte called "x"
byte x = 81;
// display a byte
System.out.println("x=" + x);
// some expressions with variable "x"
System.out.println(x & 1);
System.out.println(x | 4);
}
}