By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,584 Members | 1,803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,584 IT Pros & Developers. It's quick & easy.

value of bit in byte

P: n/a
I'm working with arrays of bytes that represent very long binary numbers.
Right now, I'm using using System.Math.DivRem by the appropriate power of 2
to get a given bit's value.

But I've got a feeling this is going to be to slow.

What's a faster way of grabbing a bit's value ?
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"John A Grandy" <johnagrandy-at-yahoo-dot-com> wrote:
I'm working with arrays of bytes that represent very
long binary numbers. Right now, I'm using using
System.Math.DivRem by the appropriate power of 2
to get a given bit's value.
But I've got a feeling this is going to be to slow.
What's a faster way of grabbing a bit's value ?


I haven't got a benchmark, but I assume the standard bitwise operators
would be faster. (If nothing else, you'd lose the method call
overhead.) You can use & to mask bits, | to combine bit patterns, and
<< and >> for bit shifts.

P.
Nov 17 '05 #2

P: n/a
On Sun, 9 Oct 2005 11:58:37 -0700, "John A Grandy"
<johnagrandy-at-yahoo-dot-com> wrote:
I'm working with arrays of bytes that represent very long binary numbers.
Right now, I'm using using System.Math.DivRem by the appropriate power of 2
to get a given bit's value.

But I've got a feeling this is going to be to slow.

What's a faster way of grabbing a bit's value ?


static int GetBit(int n, int index)
{
return (n & (1 << index)) >> index;
}

This is the simplest and fastest method I think. It creates a bitmask
for the appropriate bit, uses AND to apply the bitmask and finally
shifts the bit so the final result becomes 1 or 0.

Also, you may want to look at the class System.Collections.BitArray.
It may or may not suit your purpose.

--
Marcus Andrén
Nov 17 '05 #3

P: n/a
If you use bit masks I doubt that this will be a bottleneck, but then i
don't
know how much bit manipulation you are doing. If all else fails you can
just
write in IL which looks like this:

// TestILAssembler.il
// 11.28.04 Jeff Louie
.assembly extern mscorlib {}
.assembly TestILAssembler {.ver 1:0:1:0}
.method private static void Main(string[] args) cil managed
{
.entrypoint
.custom instance void
[mscorlib]System.STAThreadAttribute::.ctor() =
( 01 00 00 00 )
.maxstack 4
.locals init ([0] int32 n_counter,[1] int32 n_input)
.try // get user input
{
ldstr "Enter number: "
call void
[mscorlib]System.Console::Write(string)
call string [mscorlib]System.Console::ReadLine()
call int32 [mscorlib]System.Int32::Parse(string)
// parse may throw exception
stloc.1 // place valid user input into n_input
leave.s got_value // leave don't branch from
try
} // end .try
catch [mscorlib]System.Object
{
pop // remove exception from stack
ldstr "Invalid Number."
call void
[mscorlib]System.Console::WriteLine(string)
leave exit // invalid input so exit program
} // end handler
got_value: ldloc.1 // push value n_input onto stack
dup
call void [mscorlib]System.Console::Write(int32)
ldstr " [Input value] "
call void
[mscorlib]System.Console::WriteLine(string)
// **** (for int i=0; i<32; i++) ****
load_counter: ldc.i4 0 // initialize n_counter 0
stloc.0 // store in local variable n_count
begin_loop: ldloc.0 // get current counter value
ldc.i4 32
bge end_loop // break loop if counter is >=32
dup
ldc.i4 0x80000000
// bit mask 10000000000000000000000000000000
and // clear all bits except most significant
ldc.i4 0x80000000
ceq // push 1 if most sig bit is true, else 0
call void [mscorlib]System.Console::Write(int32)
ldc.i4 1 // push 1
shl // *** shift left 1, zero least sig bit
****
ldloc.0 // get n_counter
ldc.i4 1
add // add 1 to n_counter, postfix increment
stloc.0 // store new n_counter value
br begin_loop // branch to loop again
// **** end for loop ****
end_loop: pop // remove input value should be all zeros
ldstr " [32 bit value] "
end_algo: call void
[mscorlib]System.Console::WriteLine(string)
exit: ldstr "JAL 11.25.04"
call void
[mscorlib]System.Console::WriteLine(string)
ret
} // end of method Main

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.