bytes, bits, trues and falses..
-
hey there.. i have a problem working with an older data type that i dont know how to handle.. i have a byte (8 bits) which tells me if 8 different options are turned on or off.. so when i read in a value of 36 i know that bit 6 and bit 3 are on (32 + 4 = 36).. im trying to figure out an easy way to get if a bit is on or off without having to write some sort of code to parse the byte value.. so if i have:
byte bByte = GetByte(); // bByte=36 in this example
i want to be able to write a function (or use one that exists) to call something like:
if (IsOption1On(bByte))
...can someone help me understand how this works? thanks in advance! still a newb.. cut me some slack :P -dz
-
hey there.. i have a problem working with an older data type that i dont know how to handle.. i have a byte (8 bits) which tells me if 8 different options are turned on or off.. so when i read in a value of 36 i know that bit 6 and bit 3 are on (32 + 4 = 36).. im trying to figure out an easy way to get if a bit is on or off without having to write some sort of code to parse the byte value.. so if i have:
byte bByte = GetByte(); // bByte=36 in this example
i want to be able to write a function (or use one that exists) to call something like:
if (IsOption1On(bByte))
...can someone help me understand how this works? thanks in advance! still a newb.. cut me some slack :P -dz
You can use & operator to get what 'flags' options are set.
if ((bByte&0x01)!=0) {} // First bit is set
if ((bByte&0x02)!=0) {} // Second bit is set
if ((bByte&0x04)!=0) {} // Third bit is set
//...You can use this function to get whether is option set (nOpt is value from 0 to 7)
bool IsOptionOn(byte bByte,byte nOpt)
{
return ((bByte&(1<
Hope this helps :rolleyes:i'm only pointer to myself
-
hey there.. i have a problem working with an older data type that i dont know how to handle.. i have a byte (8 bits) which tells me if 8 different options are turned on or off.. so when i read in a value of 36 i know that bit 6 and bit 3 are on (32 + 4 = 36).. im trying to figure out an easy way to get if a bit is on or off without having to write some sort of code to parse the byte value.. so if i have:
byte bByte = GetByte(); // bByte=36 in this example
i want to be able to write a function (or use one that exists) to call something like:
if (IsOption1On(bByte))
...can someone help me understand how this works? thanks in advance! still a newb.. cut me some slack :P -dz
-
hey there.. i have a problem working with an older data type that i dont know how to handle.. i have a byte (8 bits) which tells me if 8 different options are turned on or off.. so when i read in a value of 36 i know that bit 6 and bit 3 are on (32 + 4 = 36).. im trying to figure out an easy way to get if a bit is on or off without having to write some sort of code to parse the byte value.. so if i have:
byte bByte = GetByte(); // bByte=36 in this example
i want to be able to write a function (or use one that exists) to call something like:
if (IsOption1On(bByte))
...can someone help me understand how this works? thanks in advance! still a newb.. cut me some slack :P -dz
-
You can use & operator to get what 'flags' options are set.
if ((bByte&0x01)!=0) {} // First bit is set
if ((bByte&0x02)!=0) {} // Second bit is set
if ((bByte&0x04)!=0) {} // Third bit is set
//...You can use this function to get whether is option set (nOpt is value from 0 to 7)
bool IsOptionOn(byte bByte,byte nOpt)
{
return ((bByte&(1<
Hope this helps :rolleyes:i'm only pointer to myself
this helped a bunch.. but im doing something wrong.. i dunno what it is.. it seems to work fine until i get into higher numbers.. here is whats happening:
byte myByte = 45;
bool bOn = (myByte&0x64)!=0;b0n
should be false since 64 is bigger than 45, so bit 7 should be off.. but its returningtrue
.. what am i doing wrong? still a newb.. cut me some slack :P -dz -
this helped a bunch.. but im doing something wrong.. i dunno what it is.. it seems to work fine until i get into higher numbers.. here is whats happening:
byte myByte = 45;
bool bOn = (myByte&0x64)!=0;b0n
should be false since 64 is bigger than 45, so bit 7 should be off.. but its returningtrue
.. what am i doing wrong? still a newb.. cut me some slack :P -dzProblem is that number
0x64
(this is16*6+4*1 = 100
) is in hexadecimal and64
is decimal. To learn more about this I recomend you this article[^]. i'm only pointer to myself -
Problem is that number
0x64
(this is16*6+4*1 = 100
) is in hexadecimal and64
is decimal. To learn more about this I recomend you this article[^]. i'm only pointer to myselfstupid hexidecimal.. :) i was wondering what was happening.. is there any reason why you chose to use hex in your example you gave me? the following works perfect:
bool bOn1 = (bByte&01);
bool bOn2 = (bByte&02);
bool bOn3 = (bByte&04);
bool bOn4 = (bByte&08);
bool bOn5 = (bByte&16);
bool bOn6 = (bByte&32);
bool bOn7 = (bByte&64);
bool bOn8 = (bByte&128);thanks for all of your help!! :beer: still a newb.. cut me some slack :P -dz
-
stupid hexidecimal.. :) i was wondering what was happening.. is there any reason why you chose to use hex in your example you gave me? the following works perfect:
bool bOn1 = (bByte&01);
bool bOn2 = (bByte&02);
bool bOn3 = (bByte&04);
bool bOn4 = (bByte&08);
bool bOn5 = (bByte&16);
bool bOn6 = (bByte&32);
bool bOn7 = (bByte&64);
bool bOn8 = (bByte&128);thanks for all of your help!! :beer: still a newb.. cut me some slack :P -dz
Hexadecimal rox. I think in hexadecimal :omg: I'm sorry if I confused you. i'm only pointer to myself
-
hey there.. i have a problem working with an older data type that i dont know how to handle.. i have a byte (8 bits) which tells me if 8 different options are turned on or off.. so when i read in a value of 36 i know that bit 6 and bit 3 are on (32 + 4 = 36).. im trying to figure out an easy way to get if a bit is on or off without having to write some sort of code to parse the byte value.. so if i have:
byte bByte = GetByte(); // bByte=36 in this example
i want to be able to write a function (or use one that exists) to call something like:
if (IsOption1On(bByte))
...can someone help me understand how this works? thanks in advance! still a newb.. cut me some slack :P -dz
You can use System.Collections.BitArray. Also, in Systems.Collections.Specialized, there is a 32-bit vector, which is essentially an integer wrapped in a struct.