Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

Work with bits

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SushiSean - 14 Oct 2007 22:36 GMT
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?
Arne Vajhøj - 14 Oct 2007 23:11 GMT
> I have some problems which getting bites.
> For example 247 it is 11110111 in bytes.
[quoted text clipped - 15 lines]
> 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
deerchao - 14 Oct 2007 23:14 GMT
On Oct 15, 6:11 am, Arne Vajh?j <a...@vajhoej.dk> wrote:
> > I have some problems which getting bites.
> > For example 247 it is 11110111 in bytes.
[quoted text clipped - 40 lines]
>
> Arne

Maybe it's a better idea to shift left but not right?
Because int is signed, sh.t right may expand the sign bit.
Arne Vajhøj - 14 Oct 2007 23:52 GMT
>>                         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, sh.t right may expand the sign bit.

For this code it does not matter.

Arne
Nicholas Paldino [.NET/C# MVP] - 15 Oct 2007 00:02 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I have some problems which getting bites.
> For example 247 it is 11110111 in bytes.
[quoted text clipped - 15 lines]
> i[2] = 1;
> How I can do this?
Michael C - 15 Oct 2007 04:14 GMT
>    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] - 15 Oct 2007 15:31 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>>    You can use the BitArray class, passing in the integer, like so:
>>
[quoted text clipped - 7 lines]
>
> Michael
Michael C - 16 Oct 2007 02:05 GMT
> Michael,
>
[quoted text clipped - 6 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.