How to store two binary strings in C # and use the OR operator

Input:

10101
11100

I would like to store these two lines in a data type so that I can call the |OR operator on two.

Here is my code:

        var g = new byte[2][];

        g[0] = "10101".Select(item => byte.Parse(item.ToString())).ToArray();
        g[1] = "11100".Select(item => byte.Parse(item.ToString())).ToArray();

        //won't compile
        Console.WriteLine(g[0] | g[1]);

Compiler error I get:

Cannot apply the '|' operator operands byte [] and byte []

I also tried BitArray, but that didn't seem right either. And I tried byte.Parse ("10101"), which just causes an overflow, it makes sense to me.

What I'm trying to do is OR bits of both strings and the result will be = 1, maybe I need to shift the bits in a for loop, I thought maybe I could just OR both binary representations of binary strings of equal length

, , byte [], , , , .

UPDATE

, . , , .

- , HackerRank: https://www.hackerrank.com/challenges/acm-icpc-team

" N , ACM-ICPC World Finals. , . , 2 , , ."

, Stack Overflow, :

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution {
static void Main(String[] args) {
    var inputArr = Console.ReadLine().Split(' ').Select(item => int.Parse(item)).ToArray();
    var n = inputArr[0];
    var m = inputArr[1];


    var g = new byte[n];
    var team = new List<byte>();

    var teamsKnowMax = 0;
    var maxTopics = byte.MinValue >> sizeof(byte) * 8 - m;

    for(var i = 0; i < n; i++){
        g[i] = Convert.ToByte(Console.ReadLine(), 2);
        maxTopics = maxTopics | g[i];
    }

    for(var j = 0; j < n -1; j++){
        for(var k = j+1; k < n; k++){
            var or = g[j] | g[k];
            if((or & maxTopics) == maxTopics)
                teamsKnowMax++;
        }
    }

    Console.WriteLine(Convert.ToString(maxTopics,2).ToCharArray().Count(item => item == '1'));
    Console.WriteLine(teamsKnowMax);
}

}

:

2≤N≤500 
1≤M≤500

, , 8- , , , , , .

8 , , , . , - . BitArray, -, | OR, . : https://codereview.stackexchange.com/questions/80458/acm-icpc-team-challenge-on-hackerrank-easy

   static void Main(String[] args) {
    var input = Console.ReadLine().Split(' ').Select(item => int.Parse(item)).ToArray();
    var N = input[0];
    var M = input[1];
    var maxTopics = 0;
    var maxTeams = 0;
    var bitArray = new BitArray[N];

    for(var n = 0; n < N; n++){
        bitArray[n] = new BitArray(M);

        var topics = Console.ReadLine();

        for(var m = 0; m < M; m++){
            bitArray[n].Set(m, topics[m] == '1');
        }
    }

    for(int i = 0; i < N -1; i ++){
        for(int j = i + 1; j < N; j++){
            var tempTopics = BitsOnCount(new BitArray(M).Or(bitArray[i]).Or(bitArray[j]));

            if (tempTopics > maxTopics){
                maxTopics = tempTopics;
                maxTeams = 1;
            }else if(tempTopics == maxTopics){
                maxTeams++;
            }

        }
    }

    Console.WriteLine(maxTopics);
    Console.WriteLine(maxTeams);
}

static int BitsOnCount(BitArray bitArray)
{
    var count = 0;
    foreach (var bit in bitArray)
    {
        if ((bool) bit)
            count++;
    }

    return count;
}
+4
4

, , LINQ .. .

var str1 = "10101";
var str2 = "11100";

var num1 = Convert.ToByte(str1, 2);
var num2 = Convert.ToByte(str2, 2);
var or = num1 | num2;

// We need to lookup only that bits, that are in original input values.
// So, create a mask with the same number of bits.
var mask = byte.MaxValue >> sizeof(byte) * 8 - Math.Max(str1.Length, str2.Length);
var result = (or & mask) == mask;

// True, when all bits after OR are 1, otherwise - False.
Console.WriteLine(result);
+5

, Linq,

 var orResult = g[0].Zip(g[1], (b1, b2) => b1|b2).ToArray();

, , . , , .


, - "0", linq All. false, .
 bool orResult = g[0].Zip(g[1], (b1, b2) => b1|b2).All(b => b != 0);
+3

( ) . , t |. .

using System;

public class Program
{
    public static void Main()
    {
        var str1 = "10101";
        var str2 = "11100";
        var mask = str1.Replace('0','1');
        int one = Convert.ToInt32(str1, 2);
        int two = Convert.ToInt32(str2, 2);
        int maskbit = Convert.ToInt32(mask, 2);
        int result = (one | two)^maskbit;

        if (result==0){

            Console.WriteLine("All flags set");             
        }
        else            
        {
            Console.WriteLine("Not all flags set");             
        }   
    }
}
+1

The binary OR operator is not defined in a type byte[], only on byte.

Binary | operators are predefined for integral types and bool. For integral types | computes the bitwise OR of its operands. For operands bool | calculates the logical OR of its operands; that is, a result is false if and only if both of its operands are false.

https://msdn.microsoft.com/en-us/library/kxszd0kx.aspx

0
source

Source: https://habr.com/ru/post/1615492/


All Articles