佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 479|回复: 3

来回转换二进制字符串(C#)

[复制链接]
发表于 9-3-2021 10:09 PM | 显示全部楼层 |阅读模式
本帖最后由 褐眼睛 于 10-3-2021 01:26 AM 编辑

下列C#源码展示如何转换二进制字符串(binary string)去二进制数(binary number),然后在转换回二进制字符串(binary string)。

当然它可以更改去16位或64位整数(integer).

  1.            string binaryString = "011100100111001001110011";
  2.            int G = 0;

  3.            for (int i = 0; i < binaryString.Length; i++)
  4.                G += (int)((binaryString[binaryString.Length - (i + 1)] & 1) << (i % 32));

  5.            Console.WriteLine(G); //&#8237;7500403&#8236;
  6.            binaryString = string.Empty;
  7.            
  8.            for (int i = 31; i >= 0; i--)
  9.                binaryString += (char)((((uint)G & (1 << (i % 32))) >> (i % 32)) | 48);

  10.            Console.WriteLine(binaryString); //00000000011100100111001001110011
复制代码



以下是计算二进制数里有多少个1......其实CPU指令也有POPCNT,
POPCNT — Return the Count of Number of Bits Set to 1

不过这是C#,所以无法直接使用CPU指令。

  1. uint G = 8501; //10 0001 0011 0101
  2. uint g = 0;

  3. for (int i = 0; i < 32; i++)
  4. {
  5. g += (G << (31 - (i % 32))) >> 31;
  6. }

  7. Console.WriteLine(g); //6
复制代码


大家有没有长见识?或者你有其他更好的方法吗?
回复

使用道具 举报


ADVERTISEMENT

发表于 11-3-2021 02:16 PM | 显示全部楼层
本帖最后由 flashang 于 11-3-2021 02:17 PM 编辑

試試看 (gcc):


  1. void popcnt() {
  2.     unsigned long G = 8501; //10 0001 0011 0101
  3.     unsigned long bits = 0;

  4.     while( G > 0 ) {
  5.         // bits = bits + (G & 1);
  6.         // G = G >> 1;

  7.         bits += (G & 1);
  8.         G >>=  1;
  9.     }
  10.     printf( "while %d \n", bits );
  11. }
复制代码





回复

使用道具 举报

 楼主| 发表于 12-3-2021 11:18 PM | 显示全部楼层

不错,你的这个也行得通。
回复

使用道具 举报

 楼主| 发表于 12-3-2021 11:19 PM | 显示全部楼层
让我放大招,如何转换十分(无限长)的二进制字符串?

  1. using System;

  2. public class GGG
  3. {
  4.     public static void Main()
  5.     {
  6.         string testCase = "0001001000000010000000000010000111000011000101100000001100000000000010010100110100100011001001000101010010001100011100110000110010000001011000100101010001100001001000010100100100000000100001010000011100000001111111111111110101000000000111110111000100000110";
  7.         //Console.Write("Please enter a binary string ('0' or '1' only): ");
  8.         //string testCase = Console.ReadLine();

  9.         uint[] G = new uint[(testCase.Length / 32) + 1];
  10.         uint g = 0;

  11.         for (int i = 0; i < testCase.Length; i++)
  12.         {
  13.             G[(i / 32)] += (uint)((testCase[testCase.Length - (i + 1)] & 1) << (i % 32));
  14.         }

  15.         for (int i = 0; i < testCase.Length; i++)
  16.         {
  17.             g += (G[(i / 32)] << (31 - (i % 32))) >> 31;
  18.         }

  19.         Console.WriteLine("There are " + g.ToString() + " occurrence(s) of '1'");
  20.         Console.WriteLine("Below are the binary numbers stored in multiple 32-bit integer starting from leftmost bit:");
  21.         for (int i = 0; i < (testCase.Length / 32) + 1; i++)
  22.         {
  23.             Console.WriteLine(G[i]);
  24.         }
  25.         Console.ReadLine();
  26.     }
  27. }
复制代码


运行结果:
There are 88 occurrence(s) of '1'
Below are the binary numbers stored in multiple 32-bit integer starting from leftmost bit:
1075802374
117571581
558432389
2170704993
1418490636
156050212
3272999680
302120993
0


这个才是我的拿手绝技,哈哈,见笑了。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 20-4-2024 10:52 AM , Processed in 0.051612 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表