Haim Evgi
Haim Evgi

Reputation: 125476

c# - represent array of integers in bit field

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# ?

Upvotes: 0

Views: 570

Answers (1)

cdhowie
cdhowie

Reputation: 168988

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.

Upvotes: 2

Related Questions