Extract Binary Data From Word Value

Just starting out? Need help? Post your questions and find answers here.
Effigy
User
User
Posts: 35
Joined: Fri Apr 25, 2003 5:40 pm
Location: Canada
Contact:

Extract Binary Data From Word Value

Post 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.
Derek Gavey
- PB Newbie -
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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
I may look like a mule, but I'm not a complete ass.
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post 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 
Please correct my english
http://purebasic.developpez.com/
Effigy
User
User
Posts: 35
Joined: Fri Apr 25, 2003 5:40 pm
Location: Canada
Contact:

Post 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
Derek Gavey
- PB Newbie -
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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
BERESHEIT
User avatar
Comtois
Addict
Addict
Posts: 1432
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post 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))
Please correct my english
http://purebasic.developpez.com/
Post Reply