U
U
Username2015-11-18 23:46:13
Programming
Username, 2015-11-18 23:46:13

What is the binary data type in C#?

Good afternoon. You need to implement bitwise or and chose a variable of type bool to work:

bool[] st = new bool[] { false, true, true, true, false, true, false, false, true, true, true, true};

But false annoys me, true I want to see the usual 0, 1. But although these are all whims. What would you recommend for working with binary numbers?
Ps The bool array was chosen because of the convenience of implementation (see screenshot), i.e. I need to do bitwise or until all 11 zeros are used.42f02796ce6d40ce95b3ee5241eac737.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Vitrenko, 2015-11-18
@Vestail

System.Data.Linq.Binary

M
Melz, 2015-11-19
@melz

Well, whatever, although they usually do something like this

unsigned int myAdd(unsigned int a, unsigned int b)
{
    unsigned int carry = a & b;
    unsigned int result = a ^ b;
    while(carry != 0)
    {
        unsigned int shiftedcarry = carry << 1;
        carry = result & shiftedcarry;
        result ^= shiftedcarry;
    }
    return result;
}

A
Alexey Pavlov, 2015-11-19
@lexxpavlov

public struct MyBool
{
    private byte _val;

    public byte Value
    {
        get { return _val; }
        set { _val = (byte)(value == 0 ? 0 : 1); }
    }

    public MyBool(int a)
    {
        _val = (byte)(a == 0 ? 0 : 1);
    }

    public override string ToString()
    {
        return _val == 0 ? "0" : "1";
    }

    public override int GetHashCode()
    {
        return _val;
    }

    public override bool Equals(object obj)
    {
        return _val == ((MyBool)obj).Value;
    }

    public static implicit operator MyBool(int a)
    {
        return new MyBool(a);
    }

    public static implicit operator int(MyBool a)
    {
        return a._val;
    }
}

MyBool[] arr1 = {1, 0, 1, 1, 0, 0, 0, 1};
MyBool[] arr2 = {1, 0, 2, 3, 0, 0, 0, -5}; 
// автоматически преобразуется в {1, 0, 1, 1, 0, 0, 0, 1}
// во всех си-подобных языках не-ноль - это истина

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question