Bit Operation

Post your coding questions such as 'how to' here.
Post Reply
peter
Active Member
Posts: 123
Joined: Mon Oct 22, 2007 2:31 pm

Bit Operation

Post by peter » Mon May 28, 2012 6:07 pm

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.

User avatar
Latexi95
Guru
Posts: 1163
Joined: Sat Sep 20, 2008 5:10 pm
Location: Lempäälä

Re: Bit Operation

Post by Latexi95 » Mon May 28, 2012 8:59 pm

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 l3
EndFunction

Function 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 l3
EndFunction


Function 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 l3
EndFunction
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 24
End Function 

Function GetColorG(sys_Col) //GREEN component of a color.
    Return (sys_Col Shl 16) Shr 24
End Function 

Function GetColorB(sys_Col) // blue component of a color.
    Return (sys_Col Shl 24) Shr 24
End Function

peter
Active Member
Posts: 123
Joined: Mon Oct 22, 2007 2:31 pm

Re: Bit Operation

Post by peter » Mon May 28, 2012 10:21 pm

Hi Latexi95,

Thank you very much.

peter
Active Member
Posts: 123
Joined: Mon Oct 22, 2007 2:31 pm

Re: Bit Operation

Post by peter » Mon May 28, 2012 10:55 pm

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.

User avatar
Latexi95
Guru
Posts: 1163
Joined: Sat Sep 20, 2008 5:10 pm
Location: Lempäälä

Re: Bit Operation

Post by Latexi95 » Mon May 28, 2012 11:28 pm

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. :D

User avatar
koodaaja
Moderator
Moderator
Posts: 1583
Joined: Mon Aug 27, 2007 11:24 pm
Location: Otaniemi - Mikkeli -pendelöinti

Re: Bit Operation

Post by koodaaja » Tue May 29, 2012 6:48 am

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.

User avatar
Misthema
Advanced Member
Posts: 312
Joined: Mon Aug 27, 2007 8:32 pm
Location: Turku, Finland
Contact:

Re: Bit Operation

Post by Misthema » Mon Sep 17, 2012 2:12 pm

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 value
color2 = _red Shl 16 + _green Shl 8 + _blue

// Both colors works the same

Text 10,60, "color1 = "+color1
Text 10,75, "color2 = "+color2

Color 0,0,color1
Box 10,10,20,20

Color 0,0,color2
Box 40,40,20,20

DrawScreen
WaitKey


// 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 24

Print "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 24

Print "Color2: "+_alpha+", "+_red+", "+_green+", "+_blue

WaitKey
Hope this helps someone.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest