问题
Does anyone know how I can solve this problem ? any help would be great...... i cant seem to get my head around it.
As you know Binary can only be either 1 or 0
Say you had a 8 digit Binary number like a byte >>>>>> 0001 1000
Im trying to figure out an equation or what would be the maximum amount of combinations you could get from an 8 digit number
What i mean is.... say you had a two digit binary number,the maximum Bnary combinations that you could have are either
00
01
10
11
Therefore total maximum combinations from a 2 digit Binary number = 4
Example 2
If you had a 3 digit number , maximum Binary Combinations would be
000
001
010
100
101
111
110
011
Therefore total maximum Binary combinations from a 3 digit number = 8
Example 3
If it were a 4 digit number, maximum binary combinations that you could have are either
0000
0001
0010
0100
1000
0111
0110
1111
1110
1101
1011
1001 Total maximum combination = 12
I Recently Asked this Question and it was answered thank you manu-fatto and zgnilec they were kind enough to let me know it was a simple equation the answer/equation is 2^digit size.
I guess my next problem is how do i write a small program that can show these combinations in Xcode or NSLog. I'm good with objective C an output I can view like NSLog would be great.
All I know is it would look something like:
int DigitSize=8
int CombinationTotal = 2^8
CombinationSize = NSMutableArray ArraywithCapacity 8;
Output
NSString Combination1 =@"0000 0000";
NSString Combination2 =@"0000 0001";
NSString Combination3 =@"0000 0010";
Nslog @"combination 1 = %@ ,Combination1";
Nslog @"combination 2 = %@ ,Combination2";
Nslog @"combination 3 = %@ ,Combination3";
……
Nslog @"combination 256 = ???? ???? ";
Sorry for the vague language I only started learning programming 3 months ago and I still have a lot of tutorials to go through.
**Im trying to build a data compression algorithm...
basically data compression is about reducing the number of bits ... the lesser the bits the smaller a file is
ie
A file with 700bits is smaller than a file with 900bits
8 bits = 1 byte
1024bytes = 1kb
1024kb = 1 mb
i donno if its even possible but i just thought what if you had an algorithm that could read 1024 bits at a time ...with the equation thats = 2^1024 = math error :( == total number of bit combinations possible
Once you have the total number of combinations you set each combination to a symbol like eg 000101010010101011001011011010101010140010101101000000001110100101100001010100000......0011010 = symbol #
So from now on whenever the computer sees the symbol # it recognises it is equal to the binary number 000101010010101011001011011010101010140010101101000000001110100101100001010100000......0011010
to better understand it ...just think of number plates on a car/vehicle, they are only a few characters but wen you punch them into police database or any car data base more information comes out its the same principle....
basically the symbols are a key to more data
i dont know if it make sense but... in theory if you could read 8388608 bits at a time
8388608 bits = 1megabyte ......
ten symbols could mean 10mb...you could create digital media 2d barcodes its just a thought i had watching starGate lol :)**
回答1:
2 to the power of 8, where 8 is number of digits.
Edit- only read first question :)
create function that will display an integer as binary
for (i = 0; i < pow(2,n), i++)
{
displayBits(i);
}
回答2:
A quick implementation
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSUInteger length = 8; // number of digits
NSUInteger n = pow(2, length); // number of possible values
for (int i = 0; i < n; i++) {
NSString *repr = @"" ;
for (int j = 0; j < length; ++j) {
if([repr length] % 5 == 0)
repr = [@" " stringByAppendingString:repr]; // add a blank after evey 4th digit
int x =( i >> j) &1;
repr = [[NSString stringWithFormat:@"%u",x] stringByAppendingString:repr];
}
NSLog(@"%@", repr);
}
}
return 0;
}
Output
0000 0000
0000 0001
0000 0010
0000 0011
0000 0100
0000 0101
0000 0110
0000 0111
0000 1000
0000 1001
0000 1010
0000 1011
0000 1100
0000 1101
0000 1110
0000 1111
0001 0000
…
1110 1100
1110 1101
1110 1110
1110 1111
1111 0000
1111 0001
1111 0010
1111 0011
1111 0100
1111 0101
1111 0110
1111 0111
1111 1000
1111 1001
1111 1010
1111 1011
1111 1100
1111 1101
1111 1110
1111 1111
The core of this program is this:
for (int i = 0; i < n; i++) {
//…
for (int j = 0; j < length; ++j) {
int x =( i >> j) &1;
//…
}
}
this will run for i =0 bis (2^n)-1 and in the inner for-loop for every j of the n bits, to check, if the least bit is 1, and append it to the representation string.
As you are a beginner, you probably dont know, what this means: int x =( i >> j) & 1;
>> shifts the bits of the left side integer by as many decimal places as the right side defines. and & 1 performs a bit wise addition
so for i == 3 and n == 8
3 as binary string representation
j = 0: 00000011 >> 0 -> 0000 0011
&0000 0001
-----------
00000 0001 -> 1 repr = 1
j = 1: 00000011 >> 1 -> 000 0001
&0000 0001
-----------
00000 0001 -> 1 repr = 11
j = 2: 00000011 >> 2 -> 00 0000
&0000 0001
-----------
00000 0000 -> 0 repr = 011
j = 3: 00000011 >> 3 -> 0 0000
&0000 0001
-----------
00000 0000 -> 0 repr = 0011
(the same till j = 7) repr = 0000 0011
来源:https://stackoverflow.com/questions/14927833/binary-numbers-what-is-the-solution-v2