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 7692979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:58:42+00:00 2026-05-31T20:58:42+00:00

For this exercise I’m making I want a decimal < 4096 to be written

  • 0

For this exercise I’m making I want a decimal < 4096 to be written in binary form in an int array.

So for example, 4 would be {0,0,0,0,0,0,0,0,0,1,0,0}. I need this for (nearly) all integers up to 4096, so I’ve written this piece of code:

for(int k=0; k<4096; k++){
    int[] myNumber =  { (k / 2048) % 2, (k / 1024) % 2, (k / 512) % 2, (k / 256) % 2, (k / 128) % 2, (k / 64) % 2, (k / 32) % 2, (k / 16) % 2, (k / 8) % 2, (k / 4) % 2, (k / 2) % 2, (k / 1) % 2 }
    /* Some processing */
}

This looks kind of ugly, so that’s why I’m curious to find out if there is a more elegant way of achieving this?

For the interested reader:
I chose for the array approach of storing the binary numbers, because I need to perform some shifting and addition modulo 2. I’m using an LFSR, and this is my implementation for that:

public class LFSR {

    private int[] polynomial;

    public LFSR(int[] polynomial) {
        this.polynomial = polynomial;
    }

    public int[] shiftLeft(int[] input) {
        int[] result = new int[input.length];

        int out = input[0];
        result[input.length - 1] = out;
        for (int i = input.length - 1; i > 0; i--) {
            result[i - 1] = (input[i] + polynomial[i - 1] * out) % 2;
        }

        return result;
    }

}

Any suggestions?

  • 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-31T20:58:43+00:00Added an answer on May 31, 2026 at 8:58 pm

    Some pseudo code:

    While (int i = 0; i < 12; i++) { 
       bitarray[i] = numericalValue & 0x1;
       numericalValue = numericalValue >> 1;
    }
    

    So, shifting right by one bit is division by 2, and ANDing with 1 always leaves you only with the lowest bit which is what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing this small exercise. declare @No decimal(38,5); set @No=12345678910111213.14151; select @No*1000/1000,@No/1000*1000,@No; Results
How would you approach this exercise, as a beginning programmer? It is a classroom
I can't understand how can I solve this exercise. I need to make a
The point of this exercise is to make navigation between objects stateful. For example,
I need help with this exercise where I have to translate properties from English
I'm stuck on this exercise. Any help would be appreciated! Thanks in advance! A
I'm stuck on a portion of the Java tutorial, specifically this exercise . The
This is an exercise for the CS guys to shine with the theory. Imagine
This is a shameless information gathering exercise for my own book. One of the
This is my first effort on solving the exercise. I gotta say, I'm kind

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.