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? 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
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.
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
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?
"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
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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%...
|
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...
|
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...
|
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...
|
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?
|
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...
|
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
|
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...
|
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...
|
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 =...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |