0

i have an array of integer like

int [] intArray;
intArray = new int[3] { 1, 2 , 40 , 45 , 50};

the array contains numbers from 1- to 50

i want to convert this array to one bit represent like

100001000010000............11

who can i do this in c# ?

1 Answer 1

2
long bitField = 0;

foreach (int bit in intArray)
    bitField |= 1l << (bit - 1);

This answer assumes 1-based bit numbers as per your question. If you would like 0 to refer to the first bit, simply change (bit - 1) to bit.

Sign up to request clarification or add additional context in comments.

1 Comment

An alternative: long bitField = intArray.Aggregate(0l, (a, x) => a | (1l << (x - 1)));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.