Looking for a method to calculate what number are used to make a number

  • Thread starter Thread starter ramscards05
  • Start date Start date
  • Tags Tags
    Method
Click For Summary
To determine which predetermined numbers sum up to a given total, convert the number to its binary representation. Each '1' in the binary string corresponds to a power of 2, indicating which of the predetermined numbers were used. For example, the binary representation of 530 is 1000010010, which shows that 512, 16, and 2 are included in the sum. Programming languages often provide bitwise operations to facilitate this process, allowing for efficient identification of set bits. Understanding binary representation is essential for accurately calculating the components that make up a specific number.
ramscards05
Messages
2
Reaction score
0
I am getting a file from a system and in one column there is a number. This number is a sum of predetermined 15 numbers.
The 15 predetermined numbers (bits) are 1 2 4 8 16 32 64 128 256 512 1024 ...,
Each predetermined number can only be used once in the total number.

So if the number is given is 530, I know 512, 16, 2 were used.

Is there a formula to calculate the values used to make the number?

Thanks,
 
Mathematics news on Phys.org
There is a key point here...

sum(k = 0 to n) of 2^k = 2^(n+1) - 1; [for example, 2^0 + 2^1 + 2^2 = 7 = 2^3 - 1]

To see how this helps, try making a number without including the greatest bit that is less than the number, eg. try making the sum reach 17 without using 16 in the sum.

This should demonstrate there is a good algorithm which will always give you the answer. (neat problem)
 
Well I am assuming you are a computer scientist. You should know how to convert a number from decimal to binary right?

530 / 2 = 265 R 0
265 / 2 = 132 R 1
132 / 2 = 66 R 0
66 / 2 = 33 R 0
33 / 2 = 16 R 1
16 / 2 = 8 R 0
8 / 2 = 4 R 0
4 / 2 = 2 R 0
2 / 2 = 1 R 0
1 / 2 = 0 R 1

So this is the bit string
1000010010

And each position in the bit string matches up to a certain power of 2.

So just reduce the number to a bit string and then get the positions of the 1s in the bit string and you will know which powers of 2 make up your number.
 
Thanks guys,
No, I am a network admin jumping out of my pool a bit :-)
 
ramscards05 said:
No, I am a network admin jumping out of my pool a bit :-)

You are network admin and you don't see binary number when you are shown one?

[URL]http://www.bpp.com.pl/IMG/faint.gif[/URL]

http://en.wikipedia.org/wiki/Positional_system
 
Last edited by a moderator:
When you have a number in an integer variable, it is already in binary and there is no need to convert it further.

Most programming languages have an AND operator which performs an AND operation between the bits of two operands. In the C programming language, the operator is called &. For example, to determine if bit 8 is set, you would use the expression something&256. Some microprocessors also have instructions that count the number of most significant or least significant zero bits. On an Intel processor, these instructions are named BSF and BSR. The BSF instruction determines the bit position of the lowest set bit in a register, while the BSR instruction determines the bit position of the highest set bit. If you are programming in assembly language on an Intel processor, you can enumerate all the bits by using the BSF instruction to find the first set bit, clear it using the BTR instruction and repeat until no more bits are found. To determine if a specific bit is set using the bit position, use the BT instruction. In the C programming lanaguage, the expression something&(1<<position) accomplishes the same function.
 
Here is a little puzzle from the book 100 Geometric Games by Pierre Berloquin. The side of a small square is one meter long and the side of a larger square one and a half meters long. One vertex of the large square is at the center of the small square. The side of the large square cuts two sides of the small square into one- third parts and two-thirds parts. What is the area where the squares overlap?

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 68 ·
3
Replies
68
Views
12K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K