Boolean array to integer?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Manticure
    New Member
    • Mar 2007
    • 10

    #1

    Boolean array to integer?

    Suppose I have a boolean array:
    Code:
     
    boolean[] b = {true, true, false, false, true, true};
    I want to convert it to a decimal number like a binary number. I this case the array represents number 110011 in decimal. So how do I convert the array to binary and then integer?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Manticure
    Suppose I have a boolean array:
    Code:
     
    boolean[] b = {true, true, false, false, true, true};
    I want to convert it to a decimal number like a binary number. I this case the array represents number 110011 in decimal. So how do I convert the array to binary and then integer?
    Hint, for the following numbers: 0, 1, 2, ... n think of this sequence instead:
    1<<0, 1<<1, 1<<2 , ... 1<<n

    kind regards,

    Jos

    Comment

    • Manticure
      New Member
      • Mar 2007
      • 10

      #3
      I'm sorry, I don't know yet about the shift operators, what they do or what they are for. Can you please explain more?

      Comment

      • merinodaniel
        New Member
        • May 2007
        • 5

        #4
        ok, this is what i think u want to do, u want to have a array with integers like bynary so when u have true is 1 and false is 0.
        well i can do this:

        boolean[] b = {true, true, false, false, true, true};
        int[] a = new int[b.lenght];
        for(int i = 0; i <= b.lenght; i++){
        if(b[i].equals(true)){
        a[i] = 1;
        }else{
        a[i] = 0;
        }
        }

        i think this is ok, sry if something wrong but i just delent de jDeveloper :P so i dont know if this java code its ok
        PM if something wrong
        bye

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Manticure
          I'm sorry, I don't know yet about the shift operators, what they do or what they are for. Can you please explain more?
          Ok, I'll rewrite my hint for you then: for the following numbers: 0, 1, 2, 3 ... n think
          of this sequence instead: 2**0, 2**1, 2**2, 2**3, ... 2**n where '**' is my
          notation for "raised to the power" (note '**' is not a Java operator).

          kind regards,

          Jos

          Comment

          • prometheuzz
            Recognized Expert New Member
            • Apr 2007
            • 197

            #6
            Originally posted by merinodaniel
            ok, this is what i think u want to do, u want to have a array with integers like bynary so when u have true is 1 and false is 0.
            well i can do this:

            boolean[] b = {true, true, false, false, true, true};
            int[] a = new int[b.lenght];
            for(int i = 0; i <= b.lenght; i++){
            if(b[i].equals(true)){
            a[i] = 1;
            }else{
            a[i] = 0;
            }
            }

            i think this is ok, sry if something wrong but i just delent de jDeveloper :P so i dont know if this java code its ok
            PM if something wrong
            bye
            No, that is not OK. The OP does not want the binary notation, but rather the decimal value. And if(b[i].equals(true)){ will not compile, nor will b.lenght.

            Comment

            Working...