Implementing a deadlock detection algorithm; issue with the loops - java

I have been looking for the past 3 hours, and I can't find the problem with this code. The user inputs the number of processes and the number of types of resources and the algorithm determines if there is a deadlock or not. I am certain that the problem is in the stepTwo() method as I have checked everything in the first block of code, and it was all fine.
Here is the pseudocode: https://www.dropbox.com/s/dt9cvk5h3ij7wqz/deadlockdetection.jpg?dl=0
Here is main, variables, etc.:
public class AssignmentIII
{
public static int numProcesses; // Represents the number of processes
public static int numResources; // Represents the number of different types of resources
public static int[] available; // Create matrix for available processes
public static int[][] allocation; // Create allocation matrix
public static int[][] request; // Create request matrix
public static int[] work; // Create work matrix
public static Boolean[] finish; // Create finish matrix
public static void main(String[] args) throws FileNotFoundException
{
try
{
Scanner scan = new Scanner(System.in);
Scanner fileScan = new Scanner(new File("input3.txt")); // Create file scanner
System.out.println("Please enter the total number of processes: ");
numProcesses = scan.nextInt();
System.out.println("Please enter the number of different types of resources: ");
numResources = scan.nextInt();
// Initalize matrices sizes
available = new int[numResources];
allocation = new int[numProcesses][numResources];
request = new int[numProcesses][numResources];
work = new int[numResources];
finish = new Boolean[numProcesses];
// Initialize the available matrix contents
for(int i = 0; i < numResources; i++)
available[i]=fileScan.nextInt();
// Initialize the allocation matrix contents
for(int j = 0; j < numProcesses; j++)
for(int k = 0; k < numResources; k++)
allocation[j][k]=fileScan.nextInt();
// Initialize the request matrix contents
for(int m = 0; m < numProcesses; m++)
for(int n = 0; n < numResources; n++)
request[m][n]=fileScan.nextInt();
// Begin deadlock detection algorithm
for(int i = 0; i < numResources; i++) // Intialize Work := Available
work[i]=available[i];
for(int j = 0; j < numProcesses; j++) // Check for Allocation != 0 and initialize Finish accordingly
{
// the Allocation matrix = 0 if the sum of all its elements = 0
// which is the same as if every element were = 0
int sumAllocation = 0;
for(int i = 0; i < numResources; i++)
{
sumAllocation += allocation[j][i];
}
if (sumAllocation != 0)
finish[j] = false;
else finish[j] = true;
}
stepTwo();
}
catch(FileNotFoundException ex)
{
System.out.println("An error has occured. The file cannot be found.");
}
}
The error is occurring somewhere in here, but I can't figure it out:
public static void stepTwo()
{
// Step 2: Find an index j where Finish[j] = false & Request[j] <= Work
for(int j = 0; j < numProcesses; j++)
{
if (finish[j] == true && j == numProcesses)
{
System.out.println("No deadlocks.");
}
for(int i = 0; i < numResources; i++)
{
if (request[j][i] <= work[i] && finish[j] == false)
{
if (i == (numResources-1))
{
finish[j] = true;
for(int m = 0; m < numResources; m++)
{
work[m] += allocation[j][m];
}
j = -1; // Reset counter
break;
}
}
else if (request[j][i] > work[i] && finish[j] == false)
{
System.out.println("P" + j + " is deadlocked.");
}
}
}
}
}
My input is:
0 0 0
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
The output I get is:
P1 is deadlocked.
P1 is deadlocked.
The correct output should be "No deadlocks."
Any help would be appreciated.

Related

Print a prime triangle in JAVA

I am trying to print a triangle of 0s and 1s. You get a number N. If N was 5 the triangle should look like:
1
11
111
11101
I get wrong output with 27 for example
1
11
111
11101
11101010
11101010001
11101010001010
11101010001010001
11101010001010001010
Lines ending with non-prime numbers are not printed, the prime numbers are printed as 1s, non-prime as 0s. I have a problem because some lines ending with 0s are printed.
import java.util.Scanner;
public class PrimeTriangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
int n = Integer.parseInt(input.nextLine());
boolean isPrime=false;
boolean nums[]=new boolean[n];
for (int i=0; i<=2; i++) {
nums[i]=true;
// System.out.print(nums[i]);
}
for (int i=4; i<=n; i++) {
int m=i/2;
for (int j=2; j<=m; j++) {
if (i%j==0) {
isPrime=false;
break;
}
else {
isPrime=true;
}
}
nums[i-1]=isPrime;
}
char[] digits = new char[n];
for (int i=0; i<n; i++) {
if (nums[i]) {
digits[i]='1';
}
else {
digits[i]='0';
}
}
for (int i=0; i<n; i++) {
if (digits[i]==1) {
System.out.println (new String (digits, 0, i+1));
/*for (int j=0; j<i; j++) {
System.out.print(digits[i]);
}
System.out.println(); */
}
}
}
}
Use Sieve of Eratosthenes to build a char[] of 0's and 1's, then print all substrings ending in 1.
static void printPrimeTriangle(int n) {
char[] primes = new char[n];
Arrays.fill(primes, '1');
for (int sqrt = (int) Math.sqrt(n) + 1, i = 1; i < sqrt; i++)
if (primes[i] == '1')
for (int prime = i + 1, j = prime * 2 - 1; j < n; j += prime)
primes[j] = '0';
for (int i = 0; i < n; i++)
if (primes[i] == '1')
System.out.println(new String(primes, 0, i + 1));
}
Test
printPrimeTriangle(80);
Output
1
11
111
11101
1110101
11101010001
1110101000101
11101010001010001
1110101000101000101
11101010001010001010001
11101010001010001010001000001
1110101000101000101000100000101
1110101000101000101000100000101000001
11101010001010001010001000001010000010001
1110101000101000101000100000101000001000101
11101010001010001010001000001010000010001010001
11101010001010001010001000001010000010001010001000001
11101010001010001010001000001010000010001010001000001000001
1110101000101000101000100000101000001000101000100000100000101
1110101000101000101000100000101000001000101000100000100000101000001
11101010001010001010001000001010000010001010001000001000001010000010001
1110101000101000101000100000101000001000101000100000100000101000001000101
1110101000101000101000100000101000001000101000100000100000101000001000101000001
....:....1....:....2....:....3....:....4....:....5....:....6....:....7....:....8
#kalina199
Here is my approach at it - I was able to reduce it to just one loop :)
package bg.Cholakov;
import java.util.Scanner;
public class PrimeTriangle {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
int[] array = new int[n];
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0, j = 1; i < n; i++, j++) {
array[i] = j;
if (isPrime(array[i])) {
array[i] = 1;
stringBuilder.append(String.valueOf(array[i]));
System.out.println(stringBuilder);
} else {
array[i] = 0;
stringBuilder.append(String.valueOf(array[i]));
}
}
}
static boolean isPrime(int number) {
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
Problems in your code:
You have set the value of isPrime to true within the loop. You can not tell if the number is prime until you have divided it with each counter of the loop and found that it is not divisible by any of the loop counters. Therefore, it should be done only when the loop is finished.
In the declaration, for (int i=4; i<=n; i++), you have declared i=4 for the number 5 i.e. you want to set nums[4] to true if 5 is prime or false if 5 is not prime. It means that you want to test 5 (not 4) for the primality. In your code, you have tested 4 for primality.
Given below is the corrected code:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the limit: ");
int n = Integer.parseInt(input.nextLine());
boolean nums[] = new boolean[n];
for (int i = 0; i <= 2; i++) {
nums[i] = true;
}
for (int i = 3; i < n; i++) {
int num = i + 1, m = num / 2, j;
for (j = 2; j <= m; j++) {
if (num % j == 0) {
nums[i] = false;
break;
}
}
// If j>m, it means that the loop did not terminate because of `break`
if (j > m) {
nums[i] = true;
}
}
// Display nums[] for testing
System.out.println(Arrays.toString(nums));
for (int j = 0; j < n; j++) {
if (nums[j] == false) {
continue;
} else {
for (int i = 0; i <= j; i++) {
if (nums[i] == true) {
System.out.print("1");
} else {
System.out.print("0");
}
}
System.out.println();
}
}
}
}
A sample run:
Enter the limit: 15
[true, true, true, false, true, false, true, false, false, false, true, false, true, false, false]
1
11
111
11101
1110101
11101010001
1110101000101
Additional notes:
I have removed an unnecessary variable, boolean isPrime. The array, boolean nums[] is sufficient in itself. I've also printed nums[] so that you can actually see what values have been set in this array.
A boolean variable has only two values, true and false. Therefore, if you check one value of a boolean variable in if, the else part becomes true for the other value e.g. in the following code, else if (nums[j]==true) is unnecessary and it can simply be written as else.
if (nums[j]==false) {
continue;
} else if (nums[j]==true) {
//...
}
A better approach:
You can make your code much cleaner by using a separate function to determine if a number is prime.
You do not need to check prime by dividing the number by each integer up to half of its value; you just need to divide the number by integers up to its square root. Check this to learn more about it.
Notice how my code looks cleaner by using the ternary operator. Check this to learn more about the ternary operator.
public class Main {
public static void main(String[] args) {
final int N = 5;
StringBuilder sb;
for (int i = 1; i <= N; i++) {
sb = new StringBuilder();
for (int j = 1; j <= i; j++) {
sb.append(j == 1 || isPrime(j) ? 1 : 0);
}
if (sb.charAt(sb.length() - 1) != '0') {
System.out.println(sb);
}
}
}
static boolean isPrime(int n) {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
Output:
1
11
111
11101
As per your requirement, even though 1 is not a prime number, a 1 should be printed for it. Also, as per this requirement, we do not need to test 0 or a negative integer for primality. Therefore, I have not put any check for 0, or 1 or a negative number in the method, isPrime to keep it clean and just relevant for this requirement.
I created an int array containing 1s and 0s and printed the triangle with two nested for loops. The outer loops is i

Transpose java error

I was trying to do a 2D array program to demonstrate a TRANSPOSE but I am getting error .. here is my code.
import java.util.Scanner;
/* To demonstrate TRANSPOSE USING 2-D array */
public class Array_2ddd {
public static void main(String args[]) {
Scanner s1 = new Scanner(System.in);
int i, j;
int myArray1[][] = new int[9][9];
int myArray2[][] = new int[9][9];
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
System.out.println("Enter array from 1 to 9");
myArray1[i][j] = s1.nextInt();
System.out.print("your array is" + myArray2[i][j]);
}
}
// Transposing now...
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
myArray2[i][j] = myArray1[j][i];
}
}
// After transposing
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
System.out.print("Your array is as follow" + myArray2[i][j]);
}
}
}
}
EDIT: My error during runtime (Solved)
EDIT 2: Solved
EDIT 3: The loop is in infinity ..it keeps on asking for values fromt the user even when i wrote i<9 and j<9..it still keeps on asking for values till infinity..
There are several errors in your code, also it is recommend that the dimensions of the array is to be declared as a final int, so your code works for all matrix sizes and that debugging is easier. In your original code, the errors are:
At the input step, you are printing one element of myArray[2] before you perform the transpose. That means, you are getting your array is0.
In the section commented "After transposing", you are outputting your array wrong. Namely, for each entry, you call System.out.print("Your array is as follow" + myArray2[i][j]);, and that you forgot to add a new line after each row (when inner loop is finished).
"..it keeps on asking for values fromt the user even when i wrote i<9 and j<9..it still keeps on asking for values till infinity.." There are 81 entries for the 9-by-9 case and you did not output which i,j index to be applied. You probably mistaken an infinite loop with a long but terminating loop.
Your transpose step is good.
Here is a refined version of your code which allows you to input array (in reading order, or more technically, row-major order), create a transposed array. You can copy and compare your current code with this code directly to test it.
public static void main(String args[]) {
final int m = 9; // Rows
final int n = 9; // Columns
Scanner s1 = new Scanner(System.in);
int i, j;
int myArray1[][] = new int[m][n]; // Original array, m rows n cols
int myArray2[][] = new int[n][m]; // Transposed array, n rows m cols
// Input
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
// Should be only prompt.
// Improved to show which entry will be affected.
System.out.printf("[%d][%d]" + "Enter array from 1 to 9\n", i, j);
myArray1[i][j] = s1.nextInt();
}
}
// Transposing now (watch for the ordering of m, n in loops)...
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
myArray2[i][j] = myArray1[j][i];
}
}
// After transposing, output
System.out.print("Your array is:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
System.out.print(myArray1[i][j] + " ");
}
System.out.println(); // New line after row is finished
}
System.out.print("Your transposed array is:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
System.out.print(myArray2[i][j] + " ");
}
System.out.println();
}
s1.close();
}
For an array with three rows (m = 3) and four columns (n = 4), I inputted the numbers from 0 to 9, and then 0, 1, 2. As expected, the output should be:
Your array is:
0 1 2 3
4 5 6 7
8 9 0 1
Your transposed array is:
0 4 8
1 5 9
2 6 0
3 7 1
You define your matrix as 9x9
int myArray1[][] = new int[9][9];
But actually you want to insert 10x10 items:
for (i = 0; i <= 9; i++)
{
for (j = 0; j <= 9; j++)
So either:
Redefine your arrays to store 10x10 items
int myArray1[][] = new int[10][10];
Only read and store 9x9 items in your defined array
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++)
You haven't close your first outer for loop i.e in line 17 and change your array size to 10,as you wanted take 10 input (for 0 to 9 = 10 values).

Construct an N input OR Gate. An OR Gate returns 0 if all its inputs are 0, otherwise 1 using java

Exact questionhttp://www.practice.geeksforgeeks.org/problem-page.php?pid=1335
Construct an N input OR Gate. An OR Gate returns 0 if all its inputs
are 0, otherwise 1.
Input:
3 //these are testcases
2 //these are number of inputs
1 1 //these are inputs
3 //these
0 0 0
4
1 1 1 0
output: 1 //o/p for 1st case
0 //o/p for second case
1
this code is causing run time error
errors such as Scanner java:907,java:1530,java:2160
class GFG {
static int or(int a[] , int n , int x){
for(int i = 0;i<n;i++)
{
if(x==0)
return 0;
}
return 1;
}
public static void main (String[] args) {
int a[] = new int[100];
int x= 0;
Scanner in =new Scanner(System.in);
int t = in.nextInt();
while(t>0){
int n = in.nextInt();
for(int i =0; i<n;i++){
a[i] = in.nextInt();
x =x+(a[i]|a[i+1]);
}
System.out.println(or(a,n,x));
t--;
}
}
}
Well your code works for the most part but:
for(int i = 0; i < n; i++) {
if(x == 0)
return 0;
}
return 1;
is equivalent to:
if(x == 0) return 0; else return 1;
What you want is:
for(int i = 0; i < n; i++) {
if(a[i] == 0)
return 0;
}
return 1;
In fact you could eliminate x altogether, but if you wanted to use x it wouldn't be necessary to store the numbers in a.
Version without array:
public static void main (String[] args) {
int x = 0; // assume x is 0.
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t > 0) {
int n = in.nextInt();
x = 0; // reinitialise x each time.
for(int i = 0; i < n; i++) {
// if and only if all inputs are 0 will x be 0.
// if any input is 1 -> x | 1 is 1 no matter what x. So the result will be 1.
x = x | in.nextInt();
}
System.out.println(x);
t--;
}
in.close();
}
The input dialog box:
You can use the sample input provided by the site.

How to display a result from a class?

I've been working on a program which multiplies matrices using threads. I written the program in a non-threaded fashion and it was working like a charm. However when I was writing it w/ threading function, it appears that I am not getting a result from the Threading class (I will rename it later on). Also if I were to use 1 thread, I would get all 3 matrices followed by a empty results for the matC matrix. Anything more than 1 would get me a Array index out of bounds.
Still pretty inept using classes and whatnot, and i apologize in advance of being somewhat wordy. But any help would be appreciated.
Main class:
package matrixthread;
import java.io.*;
import java.util.*;
public class MatrixThread extends Thread{
public static void matrixPrint(int[][] A) {
int i, j;
for (i = 0; i < A.length; i++) {
for (j = 0; j < A.length; j++) {
System.out.print(A[i][j] + " ");
}
System.out.println("");
}
}
public static void matrixFill(int[][] A) {
int i, j;
Random r = new Random();
for (i = 0; i < A.length; i++) {
for (j = 0; j < A.length; j++) {
A[i][j] = r.nextInt(10);
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int threadCounter = 0;
System.out.print("Enter number of rows: ");
int M = in.nextInt();
System.out.print("Enter number of threads: ");
int N = in.nextInt();
Thread[] thrd = new Thread[N];
if (M < N)
{
System.out.println("Error! Numbers of rows are greater than number of threads!");
System.exit(0);
}
if(M % N != 0)
{
System.out.println("Error! Number of rows and threads aren't equal");
System.exit(0);
}
int[][] matA = new int[M][M];
int[][] matB = new int[M][M];
int[][] matC = new int[M][M];
try
{
for (int x = 0; x < N; x ++)
for (int y = 0; y < N; y++)
{
thrd[threadCounter] = new Thread(new Threading(matA, matB, matC));
thrd[threadCounter].start();
thrd[threadCounter].join();
threadCounter++;
}
}
catch(InterruptedException ie){}
long startTime = (int)System.currentTimeMillis();
matrixFill(matA);
matrixFill(matB);
//mulMatrix(matA, matB, matC);
long stopTime = (int)System.currentTimeMillis();
int execTimeMin = (int) ((((stopTime - startTime)/1000)/60)%60);
int execTimeSec = ((int) ((stopTime - startTime)/1000)%60);
System.out.println("\n" + "Matrix 1: ");
matrixPrint(matA);
System.out.println("\n" + "Matrix 2: ");
matrixPrint(matB);
System.out.println("\n" + "Results: ");
matrixPrint(matC);
System.out.println("\n" + "Finish: " + execTimeMin + "m " + execTimeSec + "s");
}
}
And here's my threading class:
package matrixthread;
public class Threading implements Runnable {
//private int N;
private int[][] matA;
private int[][] matB;
private int[][] matC;
public Threading(int matA[][], int matB[][], int matC[][]) {
this.matA = matA;
this.matB = matB;
this.matC = matC;
}
#Override
public void run() {
int i, j, k;
for (i = 0; i < matA.length; i++) {
for (j = 0; j < matA.length; j++) {
for (k = 0; k < matA.length; k++) {
matC[i][j] += matA[i][k] * matB[k][j];
}
}
}
}
}
Here are my results using 2 rows and 1 thread
Matrix 1:
7 8
4 5
Matrix 2:
3 0
1 5
Results:
0 0
0 0
You have a large number of problems here. First and foremost is this loop:
for (int x = 0; x < N; x ++)
for (int y = 0; y < N; y++) {
thrd[threadCounter] = new Thread(new Threading(matA, matB, matC));
thrd[threadCounter].start();
thrd[threadCounter].join();
threadCounter++;
}
You run this loop before calling matrixFill. matA and matB are both equal to the Zero matrix at this point.
You then, for each Thread, call join() which waits for completion. So all you do is 0 * 0 = 0 N2 times.
Creating threads in a loop and calling join() on them as they are created waits for each thread to finish. This means that you never have two tasks running in parallel. This isn't threading, this is doing something with a single thread in a very complicated manner.
You then fill matA and matB and print out all three. Unsurprisingly, matC is still equal to the Zero matrix.
Your next issue is with your threading:
public void run() {
int i, j, k;
for (i = 0; i < matA.length; i++) {
for (j = 0; j < matA.length; j++) {
for (k = 0; k < matA.length; k++) {
matC[i][j] += matA[i][k] * matB[k][j];
}
}
}
}
Each thread runs this code. This code multiplies two matrices. Running this code N2 times as you do just makes a single matrix multiplication N2 times slower.
If you fixed your threading (see above) so that all your threads ran concurrently then all you would have is the biggest race hazard since in creation of Java. I highly doubt you would ever get the correct result.
TL;DR The reason matC is zero is because, when multiplication happens, maA == matB == 0.
First of all I don't know why you run the thread with the same data N^2 times. Possible you wanted to fill matA and matB every time with different data?
If you want to compute many data parallel you should do it in this way:
//First start all Threads
for (int x = 0; x < N; x ++)
for (int y = 0; y < N; y++)
{
thrd[threadCounter] = new Thread(new Threading(matA, matB, matC));
thrd[threadCounter].start();
threadCounter++;
}
//then wait for all
for(int i = 0; i < threadCounter; i++){
thrd[threadCounter].join();
}
Otherwise there is nothing parallel because you wait until the first Thread is ready and then do the next.
That you get a result of 0 0 0 0 is because matA and matB are 0 0 0 0 at the moment you start the threads.

How can i generate all subsets of a variable length set?

I am trying to write a program that generates all the subsets of an entered set in java. I think i nearly have it working.
I have to use arrays (not data structures)
The entered array will never be greater than 20
Right now when i run my code this is what i get:
Please enter the size of A: 3
Please enter A: 1 2 3
Please enter the number N: 3
Subsets:
{ }
{ 1 }
{ 1 2 }
{ 1 2 3 }
{ 2 3 }
{ 2 3 }
{ 2 }
{ 1 2 }
this is the correct number of subsets (2^size) but as you can see it prints a few duplicates and not some of the subsets.
Any ideas where I am going wrong in my code?
import java.util.Scanner;
public class subSetGenerator
{
// Fill an array with 0's and 1's
public static int [] fillArray(int [] set, int size)
{
int[] answer;
answer = new int[20];
// Initialize all elements to 1
for (int i = 0; i < answer.length; i++)
answer[i] = 1;
for (int a = 0; a < set.length; a++)
if (set[a] > 0)
answer[a] = 0;
return answer;
} // end fill array
// Generate a mask
public static void maskMaker(int [] binarySet, int [] set, int n, int size)
{
int carry;
int count = 0;
boolean done = false;
if (binarySet[0] == 0)
carry = 0;
else
carry = 1;
int answer = (int) Math.pow(2, size);
for (int i = 0; i < answer - 1; i++)
{
if (count == answer - 1)
{
done = true;
break;
}
if (i == size)
i = 0;
if (binarySet[i] == 1 && carry == 1)
{
binarySet[i] = 0;
carry = 0;
count++;
} // end if
else
{
binarySet[i] = 1;
carry = 1;
count++;
//break;
} // end else
//print the set
System.out.print("{ ");
for (int k = 0; k < size; k++)
if (binarySet[k] == 1)
System.out.print(set[k] + " ");
System.out.println("}");
} // end for
} // maskMaker
public static void main (String args [])
{
Scanner scan = new Scanner(System.in);
int[] set;
set = new int[20];
int size = 0;
int n = 0;
// take input for A and B set
System.out.print("Please enter the size of A: ");
size = scan.nextInt();
if (size > 0)
{
System.out.print("Please enter A: ");
for (int i = 0; i < size; i++)
set[i] = scan.nextInt();
} // end if
System.out.print("Please enter the number N: ");
n = scan.nextInt();
//System.out.println("Subsets with sum " + n + ": ");
System.out.println("Subsets: ");
System.out.println("{ }");
maskMaker(fillArray(set, size), set, n, size);
} // end main
} // end class
The value of i always goes from 0 to N-1 and then back to 0. This is not useful to generate every binary mask you need only one time. If you think about it, you need to move i only when you have generate all possible masks up to i-1.
There is a much easier way to do this if you remember every number is already internally represented in binary in the computer and everytime you increment it Java is doing the adding and carrying by itself. Look for bitwise operators.

Categories