1111000111000111111111111
0101011110100011110001111
这样的两个串如何求同样位置都是1的总数?

解决方案 »

  1.   

    a and b后取1个个数就行了
      

  2.   

    1111000111000111111111111
    0101011110100011110001111
    这样的两个串如何求同样位置都是1的总数?var
      s1,s2: string;
      i,count: Integer;
    begin
      s1 := '1111000111000111111111111';
      s2 := '0101011110100011110001111';
      count := 0;
      for I := 1 to Length(s1) do
        if (Byte(s1[1]) and Byte(s2[1])=1 then Inc(Count); 
      ShowMessage('同样位置都是1的总数:'IntToStr(Count));
    end;
      

  3.   

    for I := 1 to Length(s1) do
        if (Byte(s1[I]) and Byte(s2[I])=1 then Inc(Count);  //写错,这里是s1[I]和s2[I]
      

  4.   

    经调试!
    procedure TForm1.Button1Click(Sender: TObject);
    var
      s1,s2: string;
      i,count: Integer;
    begin
      s1 := '1111000111000111111111111';
      s2 := '0101011110100011110001111';
      count := 0;
      for I := 1 to Length(s1) do
        if (StrToInt(s1[I]) and StrToInt(s2[I]))= 1 then Inc(Count);
      ShowMessage('同样位置都是1的总数:'+IntToStr(Count));
    end;
      

  5.   

    var
      s1,s2: string;
      i,count: Integer;
    begin
      s1 := '1111000111000111111111111';
      s2 := '0101011110100011110001111';
      count := 0;
      for I := 1 to Length(s1) do
        if (StrToInt(s1[I]) and StrToInt(s2[I]))= 1 then Inc(Count);
      ShowMessage('counts:'+IntToStr(Count));
    end;
      

  6.   

    如何在视图中写? 
    SELECT          CAST(str1 AS binary) AS Expr1, CAST(str2 AS binary) AS Expr2, len(REPLACE(CAST(CAST(str1 AS binary) + CAST(str2 AS binary)) 
                                      AS varchar), '0', '') AS Expr3, id
    FROM                     dbo.Table_1LEN这个函数是我想求字串长度的,可是错了
      

  7.   

    LEN没错,是对的,但是"+"号错了不符合的和意思呀
      

  8.   

    长整型中置为1的个数的代码
    unsigned long CountBit(unsigned long X){X = (X & 0x55555555) + (X >> 1 & 0x55555555);X = (X & 0x33333333) + (X >> 2 & 0x33333333);X = (X & 0x0F0F0F0F) + (X >> 4 & 0x0F0F0F0F);X = (X & 0x00FF00FF) + (X >> 8 & 0x00FF00FF);X = (X & 0x0000FFFF) + (X >> 16 & 0x0000FFFF);return(X);}
    /*
    0x55555555: 01010101010101010101010101010101
    0x33333333: 00110011001100110011001100110011
    0x0F0F0F0F: 00001111000011110000111100001111
    0x00FF00FF: 00000000111111110000000011111111
    0x0000FFFF: 00000000000000001111111111111111
    */
      

  9.   

    谢谢,但是两个500位的二进制串按位与,怎么在SQL中写呀?
      

  10.   

    SELECT                  TOP 500 IDENTITY (int, 1, 1) AS id
    INTO                      table_500
    FROM                     syscolumns a, syscolumns b
    SELECT                  id, str1, str2, ISNULL
                                          ((SELECT                  COUNT(*) AS Expr1
                                                  FROM                     dbo.table_500
                                                  WHERE                   (SUBSTRING(dbo.Table_1.str1, id, 1) = '1') AND (SUBSTRING(dbo.Table_1.str2, id, 1) = '1')), 0) AS Expr1
    FROM                     dbo.Table_1 DROP TABLE table_500
      

  11.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      s1,s2: string;
      i,count: Integer;
    begin
      s1 := '1111000111000111111111111';
      s2 := '0101011110100011110001111';
      count := 0;
      for I := 1 to Length(s1) do
        if (StrToInt(s1[I]) and StrToInt(s2[I]))= 1 then Inc(Count);
      ShowMessage('同样位置都是1的总数:'+IntToStr(Count));
    end;
    ====================================================
    你用S1='1';
    s2='0';
    试下