473,326 Members | 2,010 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,326 software developers and data experts.

using a byte as a bit mask.

bob
Hi,
Having trouble determining if bit n in a byte is on or off.
Seem to remember being able to do this with the logical And operator
something like if (2 & mybyte) { do something}
Maybe I am dreaming of VB days.
Is there a succint way of doing this?
thanks
Bob

Oct 13 '08 #1
6 13899
TAB
Hi Bob

You are on the right track, try something like this
if ((bits & 1) != 0) // bit is set if not zero

where bits is an byte, uint or ulong.
Exhange 1 for the bit you would like to check, i.e. 1,2,4,8,16,32, ....

"bob" <st**************@cutthis.adriley.co.nzskrev i meddelandet
news:al********************************@4ax.com...
Hi,
Having trouble determining if bit n in a byte is on or off.
Seem to remember being able to do this with the logical And operator
something like if (2 & mybyte) { do something}
Maybe I am dreaming of VB days.
Is there a succint way of doing this?
thanks
Bob
Oct 13 '08 #2
On Oct 13, 2:10 pm, bob <startatbob_cl...@cutthis.adriley.co.nz>
wrote:
Hi,
Having trouble determining if bit n in a byte is on or off.
Seem to remember being able to do this with the logical And operator
something like if (2 & mybyte) { do something}
Maybe I am dreaming of VB days.
Is there a succint way of doing this?
thanks
Bob
It's similar here , you use & or | , note that it's only one if you
use two (&&) it's the logic operation instead of the bit operation.
Oct 13 '08 #3
public bool IsBitSet(byte value, byte bitNumber)
{
return value & (1 << bitNumber);
}

Something like that anyway :-)

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com
Oct 13 '08 #4
bob
Hi Anders,
Spot on.
Dunno why I couldn't see it.
Thanks

On Mon, 13 Oct 2008 20:49:19 +0200, "TAB" <an*************@email.com>
wrote:
>Hi Bob

You are on the right track, try something like this
if ((bits & 1) != 0) // bit is set if not zero

where bits is an byte, uint or ulong.
Exhange 1 for the bit you would like to check, i.e. 1,2,4,8,16,32, ....

"bob" <st**************@cutthis.adriley.co.nzskrev i meddelandet
news:al********************************@4ax.com.. .
>Hi,
Having trouble determining if bit n in a byte is on or off.
Seem to remember being able to do this with the logical And operator
something like if (2 & mybyte) { do something}
Maybe I am dreaming of VB days.
Is there a succint way of doing this?
thanks
Bob
Oct 14 '08 #5
Peter Morris wrote:
public bool IsBitSet(byte value, byte bitNumber)
{
return value & (1 << bitNumber);
}

Something like that anyway :-)
Close. ;)

return (value & (1 << bitNumber)) != 0;

Note, just to be overly clear: bitNumber is zero based.

--
Göran Andersson
_____
http://www.guffa.com
Oct 14 '08 #6
Effectively you just want to check a given value against x..

int value = X; //user input, or saved value
int check = 1<<3; //third to last bit 0x0...001000
bool hasCheck = (value & check == check);
I am pretty sure you can do something like the following...
[Flags]
public enum MyOptions : int {
None= 0,
First= 1,
Second= 1<<1,
Third= 1<<2
}

public bool HasOption(MyOptions value, MyOptions check) {
return (value & check == check);
}

On 10/13/2008 11:10 AM, bob wrote:
Hi,
Having trouble determining if bit n in a byte is on or off.
Seem to remember being able to do this with the logical And operator
something like if (2 & mybyte) { do something}
Maybe I am dreaming of VB days.
Is there a succint way of doing this?
thanks
Bob

--
Michael J. Ryan - tracker1(at)theroughnecks(dot)net - www.theroughnecks.net
icq: 4935386 - AIM/AOL: azTracker1 - Y!: azTracker1 - MSN/Win: (email)

.... B5: The mind sees what it needs to see. The soul sees what the soul sees.
Oct 16 '08 #7

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

Similar topics

3
by: Charles Law | last post by:
I have defined LVITEM as follows: <StructLayout(LayoutKind.Sequential)> Private Structure LVITEM Dim mask As Int16 Dim iItem As Int16 Dim iSubItem As Int16 Dim state As Int16 Dim stateMask As...
47
by: Kapil Khosla | last post by:
Hi, I am trying to reverse a byte eg. 11010000 should look like 00001011 Plz note, it is not a homework problem and I do not need the c code for it. Just give me an idea how should I proceed...
4
by: s.subbarayan | last post by:
Dear all, I would like to know the easiest efficient way to set or inject a particular value in the given word or byte?The problem is: I have to implement a function which will set a value from...
9
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
6
by: Jim H | last post by:
I have an enum that is derived from byte and a member that is of type byte. The compiler complains when I try to OR the one of the enum values with the byte. Why? public enum eDataPosition :...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
24
by: hammer1234 | last post by:
Hi I am wondering if there is a way of using the underlying bit representations of floats. I am interested in creating a violation in MISRA C:2004 of rule "The underlying bit representations...
16
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether...
2
by: Philipp Tietjen | last post by:
I have a serious problem, trying to call a C written .dll function in my c# program! The problem is I only have the already compiled .dll file and cannot look into the code. I have no problems...
7
by: dana_livni2000 | last post by:
how do i print the acuale byte sequence that represent a vairable (let say a char - i want to see the 8 bits). thanks dana
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.