## Bit Operation

Post your coding questions such as 'how to' here.

### Bit Operation

Hi,

does anyone know something about BIT OPERATION in CoolBasic?
I need an "Bit AND" and an "Bit OR". No logical operators!

for example: value = 12423936
farb = (value shr 16) BIT AND 255

if you are interested, I want to write this function for my library:

Function GetColorR(sys_col) :red component of a color.
Return sys_Col & 0xFF
End Function

Function GetColorG(sys_Col) :green component of a color.
Return (sys_Col >> 8) & 0xFF
End Function

Function GetColorB(sys_Col) : blue component of a color.
Return (sys_Col >> 16) & 0xFF
End Function

thank you for the help.
peter
Active Member

Posts: 112
Joined: Mon Oct 22, 2007 2:31 pm

### Re: Bit Operation

Bitwise operations are very slow because CB doesn't support them natively and you have to do logical operations for each bit separately. I'm not sure if these work but at least they should.
Code: Select all
`Function BinXor(l1,l2)    l3 = 0    For i = 0 To 31        l3 = l3 + (((l1 Shl (31-i) Shr 31) Xor ((l2 Shl (31-i)) Shr 31)) Shl i)    Next i    Return l3EndFunctionFunction BinAnd(l1,l2)    l3 = 0    For i = 0 To 31        l3 = l3 + (((l1 Shl (31-i) Shr 31) And ((l2 Shl (31-i)) Shr 31)) Shl i)    Next i    Return l3EndFunctionFunction BinOr(l1,l2)    l3 = 0    For i = 0 To 31        l3 = l3 + (((l1 Shl (31-i) Shr 31) Or ((l2 Shl (31-i)) Shr 31)) Shl i)    Next i    Return l3EndFunction`

But there is a better way to implement those functions which you need. You can use binary shifts to remove unwanted bits. It's much faster.
Code: Select all
`Function GetColorR(sys_col) //RED component of a color.    Return (sys_Col Shl 8) Shr 24End Function Function GetColorG(sys_Col) //GREEN component of a color.    Return (sys_Col Shl 16) Shr 24End Function Function GetColorB(sys_Col) // blue component of a color.    Return (sys_Col Shl 24) Shr 24End Function`

Latexi95
Guru

Posts: 1106
Joined: Sat Sep 20, 2008 5:10 pm
Location: Lempäälä

### Re: Bit Operation

Hi Latexi95,

Thank you very much.
peter
Active Member

Posts: 112
Joined: Mon Oct 22, 2007 2:31 pm

### Re: Bit Operation

Hi Latexi95,

yes, it works fine. But something is wrong.

Red must be SHL 24.
Blue must be SHL 8.

I am really silly, I have to open eyes in the future.
I had Bit Operation in my mind, found not this solution!

Anyway, thank you.
peter
Active Member

Posts: 112
Joined: Mon Oct 22, 2007 2:31 pm

### Re: Bit Operation

Pixel value has this kind of layout:
00000000 RRRRRRRR GGGGGGGG BBBBBBBB

If I want to get red color component I first move bits 8 steps to left (SHL 8):
RRRRRRRR GGGGGGGG BBBBBBBB 00000000
And then I move bits 24 steps to right (SHR 24):
00000000 00000000 00000000 RRRRRRRR
And here we have red color component.

Latexi95
Guru

Posts: 1106
Joined: Sat Sep 20, 2008 5:10 pm
Location: Lempäälä

### Re: Bit Operation

If you want to do it in your original way, 'x&0xFF' is equal to 'x Mod 256' and 'Shr' is obviously equal to '>>'. CB uses negative values for colors, though, so you'll have to add 256 if the end result is negative.

koodaaja
Moderator

Posts: 1567
Joined: Mon Aug 27, 2007 11:24 pm
Location: Mikkeli

### Re: Bit Operation

I know this is a bit old topic...but:

Code: Select all
`_alpha = 255_red = 255_green = 0_blue = 127// Negative integer with alpha value (CB doesn't support alpha-channels, so it's kunda useless anyway)color1 = _alpha Shl 24 + _red Shl 16 + _green Shl 8 + _blue// Positive integer without alpha valuecolor2 = _red Shl 16 + _green Shl 8 + _blue// Both colors works the sameText 10,60, "color1 = "+color1Text 10,75, "color2 = "+color2Color 0,0,color1Box 10,10,20,20Color 0,0,color2Box 40,40,20,20DrawScreenWaitKey// An example how to get color-components out from an integer (integer consists from four[4] bytes)_alpha = color1 Shr 24 // _alpha = 255_red = (color1 Shl 8) Shr 24_green = (color1 Shl 16) Shr 24_blue = (color1 Shl 24) Shr 24Print "Color1: "+_alpha+", "+_red+", "+_green+", "+_blue_alpha = color2 Shr 24 // _alpha = 0, since there isn't alpha value in color2_red = (color2 Shl 8) Shr 24_green = (color2 Shl 16) Shr 24_blue = (color2 Shl 24) Shr 24Print "Color2: "+_alpha+", "+_red+", "+_green+", "+_blueWaitKey`

Hope this helps someone.

Misthema