-
Notifications
You must be signed in to change notification settings - Fork 0
06.データタイプ
domanthan edited this page Jun 22, 2020
·
1 revision
/**
*
*/
package lesson03;
/**
* @author gridscale
*
*/
public class Calculator {
/**
* @param args
*/
public static void main(String[] args) {
//
// 2 ** 15 + ..... + 2*1 + 1 = ?
// 16 BIT整数の最大値を計算
long sum = 0;
sum = maxvalueOfBinary(16);
System.out.println("Sum of 2**15 + ... + 2**1 + 1 = " + sum );
sum = maxvalueOfBinary(20);
System.out.println("Max value of 20 bits is:" + sum);
// 32 BIT整数の最大値を計算
sum = maxvalueOfBinary(32);
System.out.println("Sum of 2**31 + ... + 2**1 + 1 = " + sum);
System.out.println("Max vaue of Integer is: " + Integer.MAX_VALUE);
System.out.println("Min value of Integer is: " + Integer.MIN_VALUE);
// int -> Integer Class
}
/**
*
* @param numberOfBits
* @return
*/
public static long maxvalueOfBinary(int numberOfBits) {
long sum = 0;
for (int i = numberOfBits-1; i >=0 ; i--) {
sum += powerOf2(i);
}
return sum;
}
/**
* 2のN次乗
*
* @param n
* @return
*/
public static long powerOf2(int n) {
long power = 1;
for (int i = 0; i < n; i++) {
power *= 2;
}
//TODO
// n = 0 -> 1;
// n = 1 -> 2;
// n = 2 -> 4;
return power;
}
}