Hi Bugbear,

I'm not that gifted in explaining, but I t'll try.

First if your question concerns the representation of binary numbers:

Then look at

http://en.wikipedia.org/wiki/Binary_numeral_system
Second:

ust a small example however just with 8 bit numbers.

Please keep in mind, that I really want to work with really big

numbers.

well an unsigned 8 bit number can have values from

0 to 255

The number 26 for example would be represented as

00011010 in binary format

For small numbers I could just use

printf("%08b",26) to get the binary string

# result should be 00011010

going from binary string representation to decimal could be done with

the oct() function

I just have to prefix the bit string with "0b" and pass it to the

oct() function.

print oct("0b"."00011010");

# result shoud be 26

the bits of this number are number from right to left so the bt on the

right is bit 0 and the bit on the left is bit7

the number 26

would have

bit 0 = 0

bit 1 = 1

bit 2 = 0

bit 3 = 1

bit 4 = 1

bit 5 = 0

bit 6 = 0

bit 7 = 0

if I want to extract bits 4 to bits 2 from my number 26 it would mean

taking

bit 2 = 0

bit 3 = 1

bit 4 = 1

which is 110 or 0b110 (0b marks a binary number in perl)

and would be equivalent to number 6 in decimal.

so I could do for example

$num = 26; #

$size=8; # tha value has a max size of 8 bits

$bitfrom=4;

$bitto=2;

$bitstring = sprintf("%0%sizeb",$num); # -> "00011010"

# the first character in this string is bit 7, tha last one is bit 0

so to get bits 4 to 2 I could now do

$bits4to2 = substr($bitstring,-$from,$from-$to+1); # -> "110"

$num4to2 = oct("0b".$bits4to2); # -> 6

I hope that clarified a little.

bye H

On Jul 30, 10:32 am, bugbear <bugbear@trim_papermule.co.uk_trim>

wrote:

> hofer wrote:

> > Hi,

>

> > Now I would like to extract certain bits out of this huge number:

>

> > Example Bits 16 bis 12 should result in 0b00001 == 0x1 == 1

> > Bits 17 bis 12 should result in 0b100001 == 0x21 == 33

>

> I don't understand your examples

>

> BugBear