473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Work with bits

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?
Oct 14 '07 #1
7 1825
SushiSean wrote:
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
Oct 14 '07 #2
On Oct 15, 6:11 am, Arne Vajhøj <a...@vajhoej.dkwrote:
SushiSean wrote:
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.

Oct 14 '07 #3
deerchao wrote:
On Oct 15, 6:11 am, Arne Vajhøj <a...@vajhoej.dkwrote:
> byte v = 247;
for(int i = 0; i < 8; i++)
{
Console.WriteLine(i + " " + ((v >i) & 1));
}
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
Oct 14 '07 #4
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 [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"SushiSean" <Su*******@discussions.microsoft.comwrote in message
news:81**********************************@microsof t.com...
>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?
Oct 14 '07 #5
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:58**********************************@microsof t.com...
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
Oct 15 '07 #6
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]
- mv*@spam.guard.caspershouse.com

"Michael C" <mi**@nospam.comwrote in message
news:OR**************@TK2MSFTNGP05.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:58**********************************@microsof t.com...
> 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

Oct 15 '07 #7
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP05.phx.gbl...
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
Oct 16 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ken | last post by:
I need to convert 32 bit Windows bitmaps to jpgs with PHP. I used the function from http://groups.google.com/groups? hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=a164f4b5.0311302128.40fb37f4%...
1
by: Scott David Daniels | last post by:
I've been working on a module to get at the bits of all numeric types (no, I haven't thought of how to solve the decimal data type that is coming). I've finally got the bits module to pass all of...
14
by: Ben | last post by:
Hi, I need to write some data types into an array of unsigned chars. These are basically "signals" within a message, so each signal will have a start bit and a length. The signals will also...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
15
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
23
by: Umesh | last post by:
This is a basic thing. Say A=0100 0001 in ASCII which deals with 256 characters(you know better than me!) But we deal with only four characters and 2 bits are enough to encode them. I want to...
11
by: Mack | last post by:
Hi all, I want to write a program to count number of bits set in a number. The condition is we should not loop through each bit to find whether its set or not. Thanks in advance, -Mukesh
77
by: borophyll | last post by:
As I read it, C99 states that a byte is an: "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment" (3.6) and that a byte...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
11
by: JoeC | last post by:
I am working on a graphics program but my question has nothing to do with graphics but trying to get an algorithm to work. I set graphics from a 16x16 grid to bits of a graphic with: bitData =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.