Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6179683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:42:46+00:00 2026-05-24T00:42:46+00:00

I have looked through as many previous questions as possible but never saw a

  • 0

I have looked through as many previous questions as possible but never saw a question that had a boolean array as a variable.

Here is my class:

public class Register {

private boolean[] register;
private int length;

    //Normal constructor
public Register(int n) {

    if (n == 8 || n == 16 || n == 32 || n == 64) {

        length = n;
        register = new boolean[length];

        for (int i = 0; i < length; i++) {

            register[i] = false;
        }

    } else {

        throw new RegisterException(
                "A register can only contain 8, 16, 32, or 64 bits");
    }

}

// Creates a copy of reg (an existing Register)
public Register(Register reg) {

    length = reg.length;
    register = new boolean[reg.register.length];

    System.arraycopy(reg.register, 0, this.register, 0, reg.register.length);
}

In my driver program i am loading “1101101” into register1, but when i do:
Register register2 = new Register(register1);

and print out both results i get:

0000000001101101

0000000000010110

Not really sure what is going on O.o any help would be appreciated, thanks!

This is my load method. i held off on putting it in here because it might be hard to read:

public void load(String binaryRep) {

    String allTheBits = binaryRep;
    int charPosition = 0;
    int loadLength;
    int binaryNum = 0;
    String index = "";
    String trimmedIndex = "";

    if (allTheBits.length() > 0 && allTheBits.length() <= length) {

        loadLength = allTheBits.length();

        for (int i = length - (loadLength); i < length; i++) {

            index = allTheBits.charAt(charPosition) + "";
            trimmedIndex = index.trim();
            binaryNum = Integer.parseInt(trimmedIndex);

            if (binaryNum == 1) {

                register[i] = true;

            } else if (binaryNum == 0) {

                register[i] = false;
            }

            charPosition++;

        }
    } else {
        throw new RegisterException("You can only load 0 - " + length
                + "bits.");
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:42:46+00:00Added an answer on May 24, 2026 at 12:42 am

    Here’s a more idiomatic way of doing it (using the Cloneable interface):

    public class Register implements Cloneable {
    
    private boolean[] register;
    
    public Register(boolean[] register) {
    
        int n = register.length;
    
        if (n == 8 || n == 16 || n == 32 || n == 64) {
            this.register = register;
        } else {
    
            throw new IllegalArgumentException(
                    "A register can only contain 8, 16, 32, or 64 bits");
        }
    
    }
    
    @Override
    public String toString() {
    
        StringBuilder builder = new StringBuilder();
    
        for ( boolean b : this.register ) {
            builder.append( b ? "1" : "0" );
        }
    
        return builder.toString();
    }
    
    public Register( int n ) {
        this( new boolean[n] );
    }
    
    public int getLength() {
        return this.register.length;
    }
    
    @Override
    public Register clone() {
    
        boolean[] clonedRegister = new boolean[this.register.length];
    
        System.arraycopy(this.register, 0, clonedRegister,0, this.register.length);
    
        return new Register( clonedRegister );
    }
    
    }
    

    And a JUnit test showing it in action:

    import org.junit.Assert;
    import org.junit.Test;
    
    
    public class RegisterTest {
    
        @Test
        public void testRegisterToString() {
    
            Register source = new Register( new boolean[] {true, true, false, false, true, false, true, false } );
    
            String result = "11001010";
    
            Assert.assertEquals( result, source.toString() );
    
        }
    
        @Test
        public void testRegisterCloning() {
    
            Register source = new Register( new boolean[] {true, true, false, false, true, false, false, false } );
            Register clone = source.clone();
    
            Assert.assertEquals( source.toString(), clone.toString() );
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've looked through several posts but I haven't quite found any answers that have
I have looked through containable component and other questions here, but doesn't look like
I have looked through many tutorials, as well as other question here on stack
I have looked through many threads and/or questions on the internet looking for a
Looked through many answers, but couldn't find one solving this. I have four (potentially
I'm still relatively new to Rails and have looked through many similar questions here,
have looked through many posts here on SO, and haven't found any that address
I have looked through many similar questions and tried the solutions with no success.
I have been looking through so many questions exactly like this, but I have
I looked through previous questions to this effect, but my situation is slightly different...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.