0

Bits Counter in java

1. User inputs a positive base 10 integer, greater than zero, determine how many one bits are in the binary equivalent. Then determine how many numbers between zero and the given number contain the same number of one bits. For example, the number 12 expressed as a binary number (1100) has 2 one bits. Between 0 and 12 there are 5 other numbers having 2 one bits when expressed as a binary number. These are: 3 (11), 5 (101), 6 (110), 9 (1001), 10 (1010). Test data: Input Output 8 8 in binary has 1 1-bits. There are 3 other numbers with 1 1-bits between 0 and 8. 12 12 in binary has 2 1-bits. There are 5 other numbers with 2 1-bits between 0 and 12. 17 17 in binary has 2 1-bits. There are 6 other numbers with 2 1-bits between 0 and 17. 55 55 in binary has 5 1-bits. There are 2 other numbers with 5 1-bits between 0 and 55. I already solve this problem but i want to know if there is more efficient way to do this. Below is the program written import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number:"); int num; int obits=0,zbits=0,tbits=0; num = input.nextInt(); int[] bin = new int[10]; int j = 0; int z=num; while (num > 0) { bin[j] = num % 2; num = num / 2; j++; } for(int i=0;i<j;i++){ if(bin[i]==1){ tbits++; } if(bin[i]==0){ zbits++; } } int total=0; int y=z-1; int[] others=new int[z]; for(int i=y;i>0;i--){ int n=i; int x=0,num1=0,zeros=0; while(n>0){ others[x] = n%2; n = n/2; x++; } for(int g=0;g<=x;g++){ if(others[g]==1){ num1+

2nd Jul 2020, 5:32 AM
Ikram Arshad
Ikram Arshad - avatar
6 Respostas
+ 3
There are a few algorithms that are considerably faster. See the link below and look for "Counting Bits Set". http://graphics.stanford.edu/~seander/bithacks.html Beware, these are not necessarily good programming practice. Avoid using arcane tricks unless you really require the speed or space savings.
2nd Jul 2020, 1:27 PM
Brian
Brian - avatar
+ 3
Martin Taylor I apologize if I appeared to be at odds with you on something. I was not. My intent is to be supportive and collaborative, but I unknowingly caused offense. There is room here for all levels of development. Many are seeking guiding steps to reach the next level. SoloLearn is a good community where seniors can gently nudge the juniors to improve through iterative growth. There is almost always an alternate way to implement a solution. Here the community encourages every developer to first think of a way to solve a problem, try a proof of concept, and then seek guidance and understanding of other solutions. That is the pattern happening here as well. If you can help Ikram Arshad get his example working correctly, then next he should look at the others in the link I posted. That will answer his question whether there is a more efficient way to do this.
2nd Jul 2020, 8:33 PM
Brian
Brian - avatar
+ 2
Martin Taylor my credentials include commercial and consumer 8-bit embedded development, 8-bit video games, 16-bit real time 2-D and 3-D high speed data collection and motor control, and even medical image processing. Those are the conditions that do require eking out precious CPU cycles and keeping low memory usage. Optimizing code is my favorite activity. My advice warns against developing code that is difficult for others to understand or maintain - or worse, copying someone else's code without understanding. Embedded work is where "you really require the speed or space savings." These tricks are not to be shunned, but used with consideration of whether they seem arcane to the developer or the team.
2nd Jul 2020, 4:51 PM
Brian
Brian - avatar
+ 1
Thanks bro
2nd Jul 2020, 6:49 AM
Ikram Arshad
Ikram Arshad - avatar
+ 1
Actually the question was for python language (in my friend's mid term exam) and i'm learning java so i try to make logic in java language. I know the code doesn't make any sense but trust me it's working😁😂😁
2nd Jul 2020, 1:31 PM
Ikram Arshad
Ikram Arshad - avatar
2nd Jul 2020, 1:33 PM
Ikram Arshad
Ikram Arshad - avatar