http://qs1969.pair.com?node_id=499354

ozboomer has asked for the wisdom of the Perl Monks concerning the following question:

I still can't get my head around this bit manipulation very well at the moment. Maybe its' just that I'm full of 'flu and I'm not seein' the obvious... Still, the current problem:

An 8-bit byte that has 3 bit-fields in it, thus:

bbbbbbbb || | || +-- D3 (4 bits) |+----- D2 (3 bits) +------ D1 (1 bit)
As an example, the value 162 decimal (10100010 binary) would give:-

D1 = 1, D2 = 2, D3 = 2
...but try as a I might, I just can't get the thing out. unpack(b1b3b4, $value) doesn't work, using vec() doesnt' help either.

I can logically "&" the value and get the bits selected out but I'd then need to do a 'rotate right' or something to get them into usable numbers.

Any pointers on how to get 'round this?