Page 1 of 1

Extract Binary Data From Word Value

Posted: Sun Mar 12, 2006 8:31 pm
by Effigy
I'm trying to extract integer values from a Binary Sequence.

For Example.

Say We have the Number 26458 in binary this looks like
110011101011010
I need to seperate the binary data into 3 Chunks each of five ex.
11001 11010 11010

This then needs to convert to
25, 26, 26 the integer values of each set of 5 binary digits.

I know I could use the bin() function but speed is critcal for this operation. My guess is that some of the bitshifting operators is the key... but I just don't get it.

Any help would be appreciated.

The reason I need this is because I need to extract the color values out of a 16bit color image. This is how its stored.
Bit 1 - Not Used
Bit 2-6 Red
Bit 7-11 Green
Bit 12-16 Blue

THanks is advance.

Posted: Sun Mar 12, 2006 9:01 pm
by srod
I don't know if the following helps but it certainly strips out the values 25, 26 and 26 from 26458.

Code: Select all

a.w = 26458
low.b = a & %11111
middle.b = a>>5 & %11111
top.b = a >> 10 & %11111

Posted: Sun Mar 12, 2006 9:06 pm
by Comtois

Code: Select all

Color = %1100111010110100
Red   = (Color & $003E) >> 1   
Green = (Color & $07C0) >> 6
Bleue = (Color & $F800) >> 11

Debug Color 
Debug bleue
Debug Green 
Debug Red 

Posted: Mon Mar 13, 2006 4:20 pm
by Effigy
Thanks for the help... I still don't understand completely why this works... anyway...

So using the same method how would I get three RGB values to go back into a long value.

Example:
Red=128
Blue=255
Green=255

FinalColorValue=$80FFFF

Posted: Mon Mar 13, 2006 6:49 pm
by netmaestro

Code: Select all

; SwapRB routine by netmaestro
; useful for importing/exporting colors
; from applications using high-order red
; in the #color (PB colors use high-order blue)

Procedure.l SwapRB(color.l)
  tmp=PeekC(@color)
  PokeC(@color,PeekC(@color+2))
  PokeC(@color+2,tmp)
  ProcedureReturn color
EndProcedure

Debug Hex(RGB(128,255,255))         ;high-order blue format
Debug Hex(SwapRB(RGB(128,255,255))) ;high-order red format

Posted: Mon Mar 13, 2006 7:06 pm
by Comtois
or

Code: Select all

Red=128
Blue=255
Green=255

Color = red << 16 | Green << 8 | Blue

Debug Hex(COLOR)
Debug Hex(RGB(Blue,Green,Red))