Work with bits

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have some problems which getting bites.
For example 247 it is 11110111 in bytes.

int i = 247; // 11110111

How I can get some pozition on bite?
Something like this
i[0] = 1;
i[1] = 1;
i[2] = 1;
i[3] = 0;
i[4] = 1;
i[5] = 1;
i[6] = 1;
i[7] = 1;

So I can get 1 or 0 for any pozition.
i[2] = 1;
How I can do this?
 
SushiSean said:
I have some problems which getting bites.
For example 247 it is 11110111 in bytes.

int i = 247; // 11110111

How I can get some pozition on bite?
Something like this
i[0] = 1;
i[1] = 1;
i[2] = 1;
i[3] = 0;
i[4] = 1;
i[5] = 1;
i[6] = 1;
i[7] = 1;

So I can get 1 or 0 for any pozition.
i[2] = 1;
How I can do this?

Welcome to the world of bit manipulation !

Try look at this code:

using System;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
byte v = 247;
for(int i = 0; i < 8; i++)
{
Console.WriteLine(i + " " + ((v >> i) & 1));
}
Console.ReadKey();
}
}
}

Arne
 
SushiSean said:
I have some problems which getting bites.
For example 247 it is 11110111 in bytes.
int i = 247; // 11110111
How I can get some pozition on bite?
Something like this
i[0] = 1;
i[1] = 1;
i[2] = 1;
i[3] = 0;
i[4] = 1;
i[5] = 1;
i[6] = 1;
i[7] = 1;
So I can get 1 or 0 for any pozition.
i[2] = 1;
How I can do this?

Welcome to the world of bit manipulation !

Try look at this code:

using System;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
byte v = 247;
for(int i = 0; i < 8; i++)
{
Console.WriteLine(i + " " + ((v >> i) & 1));
}
Console.ReadKey();
}
}

}

Arne

Maybe it's a better idea to shift left but not right?
Because int is signed, shit right may expand the sign bit.
 
Maybe it's a better idea to shift left but not right?
Because int is signed, shit right may expand the sign bit.

For this code it does not matter.

Arne
 
You can use the BitArray class, passing in the integer, like so:

BitArray bitArray = new BitArray(new int[]{i});

Now, bitArray[0] is equal to true, bitArray[3] is false, and so on, and
so on.
 
Nicholas Paldino said:
You can use the BitArray class, passing in the integer, like so:

BitArray bitArray = new BitArray(new int[]{i});

Now, bitArray[0] is equal to true, bitArray[3] is false, and so on, and
so on.

This is potentially inefficient I guess. It's possibly better to just loop
through the bits using & and <<.

Michael
 
Michael,

"better" is a subjective term, at best. If you were to ask the OP, I
would say that using a BitArray is better, since it gives him the exact
functionality he is looking for.

Also, "potentially" inefficient doesn't hold much meaning. It probably
is slower than just applying a bit mask, but to be honest, I don't know how
much slower it can be and depending on what you are doing, how significant
that performance hit will be.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael C said:
Nicholas Paldino said:
You can use the BitArray class, passing in the integer, like so:

BitArray bitArray = new BitArray(new int[]{i});

Now, bitArray[0] is equal to true, bitArray[3] is false, and so on,
and so on.

This is potentially inefficient I guess. It's possibly better to just loop
through the bits using & and <<.

Michael
 
Nicholas Paldino said:
Michael,

"better" is a subjective term, at best. If you were to ask the OP, I
would say that using a BitArray is better, since it gives him the exact
functionality he is looking for.

Also, "potentially" inefficient doesn't hold much meaning. It probably
is slower than just applying a bit mask, but to be honest, I don't know
how much slower it can be and depending on what you are doing, how
significant that performance hit will be.

That is why I used the terms "potentially", "I guess" and "possibly".
Naturally it depends on the situation but it is wise to mention that your
solution is potentially inefficient. If used in a loop the BitConverter
could possibly be 50 to 100 times slower. (No I have not tested this and it
is a guesstimate).

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top