0

In java I was attempting to make a binary counter, that later on I could use to count in any base-n system. My results however had a mysterious outcome. At one point the values jumped from 1111111111 to -1111111111. It seemed unusual, but the fact that I don't know how different number limits work in java probably doesn't help me in this situation. Meanwhile I looked at my code and so no way that the value could become negative. My code for the binary counter is as follows: `

package bin;

public class Counter {
    public static Integer currentNumber = 0;
    public static Integer upTo = 1000;
    public static Integer currentCount = 0;


    public static void main(String[] args) {


        while(upTo>=currentCount) {
            if(currentNumber.toString().endsWith("0")) {
                currentNumber++;
            }else if(currentNumber.toString().endsWith("1")){
                currentNumber+=10;
            }
            if(currentNumber.toString().contains("2")|currentNumber.toString().contains("3")|currentNumber.toString().contains("4")|currentNumber.toString().contains("5")|currentNumber.toString().contains("6")|currentNumber.toString().contains("7")|currentNumber.toString().contains("8")|currentNumber.toString().contains("9")) {

            }else {
                currentCount++;
                System.out.print(currentNumber + "\n");
            }
        }
        System.out.print("done");
    }
}

`

The results of the program can be found at https://pastebin.com/UUMRhkhv I would appreciate any answer that explains what may have happened so I may create a more efficient and accurate binary counter.

  • Is there a specific reason as to why you use the wrapper-class `Integer` instead of `int`? As to the number representation: Java uses [Two's complement representation](https://en.wikipedia.org/wiki/Two%27s_complement) for integer numbers. – Turing85 Oct 31 '17 at 20:50
  • Is 'or' not || instead of just | – achAmháin Oct 31 '17 at 20:53
  • For some reason I prefer to not use short-hand names. When I use short-hand names I end up getting confused between the long-hand and short-hand. For example I can't call a scanner scan, without trying to reference it with the name "scanner". –  Oct 31 '17 at 20:55
  • @NicksWorld what do you mean by "short-hand names"? There is a difference between `Integer` and `int`. An `Integer` is an object, an `int` is a primitive. – Turing85 Oct 31 '17 at 20:56

1 Answers1

0

You're encountering an integer overflow. An int in Java is 32-bits, so the max value is 2,147,483,647. The number you tried to store exceeds that. You might want to use a long or just keep the representation as a string.

(BTW, your binary counter doesn't actually count properly, but presumably you already know that. Also, you really want ||, which is the logical or operator instead of |, which is the bitwise or operation.)

user94559
  • 59,196
  • 6
  • 103
  • 103
  • That’s not the largest int. Also, using | is unusual but not incorrect. – cpp beginner Oct 31 '17 at 20:55
  • There is no `unsigned` in java =) – Turing85 Oct 31 '17 at 20:55
  • @Turing85 Weird. Switched to `long`. @cpp beginner, you may have been looking at a version of my answer before I edited it. – user94559 Oct 31 '17 at 20:56
  • Looks like I was. Note however that | can be applied to booleans, so calling it bitwise is a bit misleading. – cpp beginner Oct 31 '17 at 20:58
  • @cppbeginner not really. `|` and `||` have different meanings in boolean-logic: ´||´ is a short-circuit evaluation while `|` is not. If you apply the operator `|` to `int`s, however, you get a bitwise-or. It's just overloaded. – Turing85 Oct 31 '17 at 21:00
  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html calls it the "bitwise inclusive OR" operator, so I think I'm going to leave the text as-is. (It's almost certainly a mistake to use it where a logical or was intended.) – user94559 Oct 31 '17 at 21:02
  • @Turing I know what it does. The OP is applying it to booleans, so it’s not being used as a bitwise operator here. – cpp beginner Oct 31 '17 at 21:02
  • https://stackoverflow.com/a/4014559/94559 seems to cover it well. When applied to booleans, it acts as a "boolean logical operator," which behaves as @Turing85 described. – user94559 Oct 31 '17 at 21:04