[分享] binary string 與 int 互轉
這是小詹自己寫的幾個function,是自己作業延伸出來做的。
為什麼呢?因為網路上找到能用的,速度都太慢>_<,不符合我作業的需求。
提供給版上需要的人參考。
當然,如果有bug,或者有更好的寫好,也請告訴我喔~~
感謝各位為C&CPP貢獻的大大們!
1。 binary to string function (版本1)
inline int bin2int( string bin ){
int sum = 0;
for( int i=0; i<bin.length(); ++i ){
// if( bin[i] == '0' ) sum *= 2;
// else sum = sum*2+1;
sum = (sum<<1) | (bin[i]&1); // 這個比較快! XD
}
return sum;
}
這版的想法很簡單,但由於用到了C++裡的string,速度實在不夠快。
中間的 sum*=2 我有考慮換成 sum<<=2 但實驗結果沒有比較快。
(小詹的實驗很簡單,兩個function計時各跑 N 次,看哪個時間花的少就是比較快
N 可為十萬、一百萬、一千萬,越大越明顯 )
2。 binary to string function (版本2)
inline int bin2int( const char* bin, int len ){
int sum = 0;
for( int i=0; i<len; ++i ){
if( bin[i] == '0' ) sum*=2;
else sum = sum*2+1;
}
return sum;
}
這個版本跑得真的滿快的(希望能夠知道有更快的版本),估計原因在於使用C本身的char*
string,缺點就是,必須多傳一個參數len進來告知string的長度,如果用sizeof(bin),
好像會有問題。 傳進來最保險。
小詹有比過例如一個string str = "110010";
寫 bin2int( str ) 跟 bin2int( str.c_str(), str.length() ) 這兩種,真的是後者
比較快。 有興趣的朋友再自己實驗看看了。
3。 int to binary string function (第一種)
inline string int2bin( int n, int len ){
int mask = 0x01;
char pch[len];
int i = len;
memset( pch, '0', len );
while( i!=0 ){
--i;
if( n&mask ) pch[i] = '1';
n >>= 1;
}
return string( pch, len );
}
這也是我自己寫的版本,做法就是一直用 n>>=1 來讀最低的那個bit。
裡面一樣用到char的陣列,因為還是一樣,比c++的string快多了。
這個版本之所以要傳參數len,是讓使用者可以指定,回傳時的string長。
例如 int2bin( 5, 3 ) 會回傳 "101",而int2bin( 5, 5 )會回傳"00101",
在前面補了 0 。
如果不在意補0,小詹有另外一寫法,如下
4。 int to binary string (第二種)
inline string int2bin( int n ){
char bits[32];
memset( bits, '0', 32 );
int i=31;
while( n>0 ){
if( n%2 ) bits[i] = '1';
n >>= 1; // n = n/2
--i;
}
return string( &bits[i+1], 31-i );
}
這樣就只會回傳到剛剛好,不在前面補零。
這是小詹一點點的分享,感謝您的閱讀。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.218.146
→
06/20 18:36, , 1F
06/20 18:36, 1F
→
06/20 18:37, , 2F
06/20 18:37, 2F
→
06/20 18:38, , 3F
06/20 18:38, 3F
→
06/20 18:38, , 4F
06/20 18:38, 4F
※ 編輯: eejimchan 來自: 140.112.218.146 (06/20 18:39)
→
06/20 18:39, , 5F
06/20 18:39, 5F
→
06/20 18:40, , 6F
06/20 18:40, 6F
→
06/20 18:41, , 7F
06/20 18:41, 7F
→
06/20 18:42, , 8F
06/20 18:42, 8F
→
06/20 18:43, , 9F
06/20 18:43, 9F
→
06/20 18:47, , 10F
06/20 18:47, 10F
→
06/20 18:50, , 11F
06/20 18:50, 11F
→
06/20 18:51, , 12F
06/20 18:51, 12F
※ 編輯: eejimchan 來自: 140.112.218.146 (06/20 18:52)
→
06/20 18:54, , 13F
06/20 18:54, 13F
→
06/20 19:01, , 14F
06/20 19:01, 14F
→
06/20 19:06, , 15F
06/20 19:06, 15F
→
06/20 19:09, , 16F
06/20 19:09, 16F
→
06/20 19:29, , 17F
06/20 19:29, 17F
→
06/20 19:30, , 18F
06/20 19:30, 18F
推
06/20 19:42, , 19F
06/20 19:42, 19F
→
06/20 19:43, , 20F
06/20 19:43, 20F
→
06/20 19:45, , 21F
06/20 19:45, 21F
→
06/20 19:46, , 22F
06/20 19:46, 22F
→
06/20 19:48, , 23F
06/20 19:48, 23F
→
06/20 20:11, , 24F
06/20 20:11, 24F
→
06/20 20:12, , 25F
06/20 20:12, 25F
→
06/20 20:21, , 26F
06/20 20:21, 26F
→
06/20 20:43, , 27F
06/20 20:43, 27F
→
06/20 20:44, , 28F
06/20 20:44, 28F
→
06/20 20:46, , 29F
06/20 20:46, 29F
→
06/20 21:02, , 30F
06/20 21:02, 30F
→
06/20 21:03, , 31F
06/20 21:03, 31F
→
06/20 21:04, , 32F
06/20 21:04, 32F
→
06/20 21:04, , 33F
06/20 21:04, 33F
→
06/20 21:14, , 34F
06/20 21:14, 34F
→
06/20 21:16, , 35F
06/20 21:16, 35F
→
06/20 21:16, , 36F
06/20 21:16, 36F
→
06/20 21:33, , 37F
06/20 21:33, 37F
→
06/20 21:41, , 38F
06/20 21:41, 38F
推
06/20 22:28, , 39F
06/20 22:28, 39F
→
06/20 22:29, , 40F
06/20 22:29, 40F
→
06/20 22:35, , 41F
06/20 22:35, 41F
推
06/20 22:37, , 42F
06/20 22:37, 42F
→
06/20 22:37, , 43F
06/20 22:37, 43F
→
06/20 22:39, , 44F
06/20 22:39, 44F
→
06/20 22:42, , 45F
06/20 22:42, 45F
推
06/20 22:52, , 46F
06/20 22:52, 46F
→
06/20 22:52, , 47F
06/20 22:52, 47F
推
06/20 22:53, , 48F
06/20 22:53, 48F
→
06/20 22:54, , 49F
06/20 22:54, 49F
→
06/20 22:54, , 50F
06/20 22:54, 50F
→
06/20 22:55, , 51F
06/20 22:55, 51F
推
06/20 22:56, , 52F
06/20 22:56, 52F
→
06/20 22:57, , 53F
06/20 22:57, 53F
→
06/20 22:57, , 54F
06/20 22:57, 54F
→
06/20 22:58, , 55F
06/20 22:58, 55F
→
06/20 22:58, , 56F
06/20 22:58, 56F
→
06/20 22:59, , 57F
06/20 22:59, 57F
→
06/20 22:59, , 58F
06/20 22:59, 58F
→
06/20 23:00, , 59F
06/20 23:00, 59F
→
06/20 23:01, , 60F
06/20 23:01, 60F
→
06/20 23:03, , 61F
06/20 23:03, 61F
→
06/20 23:04, , 62F
06/20 23:04, 62F
→
06/20 23:04, , 63F
06/20 23:04, 63F
→
06/20 23:05, , 64F
06/20 23:05, 64F
→
06/20 23:06, , 65F
06/20 23:06, 65F
→
06/20 23:07, , 66F
06/20 23:07, 66F
推
06/20 23:18, , 67F
06/20 23:18, 67F
→
06/20 23:20, , 68F
06/20 23:20, 68F
→
06/20 23:20, , 69F
06/20 23:20, 69F
→
06/20 23:21, , 70F
06/20 23:21, 70F
→
06/20 23:22, , 71F
06/20 23:22, 71F
→
06/20 23:22, , 72F
06/20 23:22, 72F
推
06/20 23:23, , 73F
06/20 23:23, 73F
→
06/20 23:23, , 74F
06/20 23:23, 74F
→
06/20 23:23, , 75F
06/20 23:23, 75F
→
06/20 23:24, , 76F
06/20 23:24, 76F
→
06/20 23:25, , 77F
06/20 23:25, 77F
→
06/20 23:25, , 78F
06/20 23:25, 78F
→
06/20 23:26, , 79F
06/20 23:26, 79F
推
06/20 23:35, , 80F
06/20 23:35, 80F
→
06/20 23:35, , 81F
06/20 23:35, 81F
→
06/20 23:38, , 82F
06/20 23:38, 82F
→
06/20 23:39, , 83F
06/20 23:39, 83F
推
06/22 03:15, , 84F
06/22 03:15, 84F
→
06/22 23:40, , 85F
06/22 23:40, 85F
→
06/23 15:14, , 86F
06/23 15:14, 86F
→
06/23 15:15, , 87F
06/23 15:15, 87F
※ 編輯: eejimchan 來自: 140.112.42.22 (10/03 12:11)