Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Bitwise Operator Tutorial for the clueless

Author
Message
Mattman
22
Years of Service
User Offline
Joined: 5th Jun 2003
Location: East Lansing
Posted: 23rd Jan 2006 18:39 Edited at: 23rd Jan 2006 18:41
I saw AND's, OR's, and XOR's used on numbers in a C++ tutorial I was hoping to port to DBP the other day and didn't really have a clue what they did. So I thought I would share my new knowledge with those unfamiliar with bitwise operators.

First off, binary number system. If you have a basic understanding of how it works, skip this paragraph. If not, read on. Our number system has 10 digits, 0-9. When we hit 09, we increment by one to 10, then 11, 12, 13...up to 19, then to 20, etc. In binary, there are two digits: 0 and 1. 0 in binary would be 0. 1 would be 1. But 2 WOULD NOT be 2, because the binary system does not make use of the digit 2. We would go to 10. Then 11 for 3. Get it? If not, look for more in-depth tutorials on google. This is the best I can explain it for now.

There are three bitwise operators I will cover right now, AND, OR, and XOR. The purpose of the AND command is to put 0's into a binary number. For example, we have the binary number 000110 and we want to make sure the middle 4 digits become 0's. We will AND the number. Bitwise operators take two digits of the same place from two seperate binary numbers, compares them, then makes a new number from the results. When ANDing, the second number is the major force. If the digit in the second number is a 1, the resulting number for that digit is the same as what the digit was in the first number. If it is a 0, then the resulting digit becomes a 0. The second binary number can be thought of as a filter, with each digit being a slot. 1 slots are open, letting the first number come through, while 0 slots block data off, ending in a 0.

Example in DBP: 001100 && 100100 = 000100

OR's do the opposite of AND's. 1 slots result in 1's, while 0 slots retain original data. This is because in an OR, the resulting data is NUMBER 1 OR NUMBER 2, so if either is a 1 then it results in a 1. Regardless of the original number, the second number makes it a 1 everytime. 0's result in 1 or 0 depending on the original data however.

Example in DBP: 001100 II 100100 = 101100

XOR's are used to invert data. XOR is similar to OR, but it is checking to see if one is a 1 and the other is a 0. 0's retain original data once again and 1's reverse, or invert the data.

Example in DBP: 001100 ~~ 100100 = 101000

Hopefully that is a good intro, I wrote this in 15 minutes during my lunch period. I will be back tonight to fix up stuff and add more! Comments much appreciated.

Matt

Why make sense when you could make brownies?
Milkman
19
Years of Service
User Offline
Joined: 30th Nov 2005
Location: United States
Posted: 23rd Jan 2006 19:22
pretty good tutorial, but could you give a few examples as to what this could be used for in an actual program?

"Genius is 1% inspiration and 99% perspiration"
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 23rd Jan 2006 20:49
It's handy for sorts of stuff.. One that comes to mind for most people is dealing with RGB values.


NOTE: These examples use numbers in HEX format (HEx is base 16 number system). And the Code is completely untested - Beware


Example:

C=Rgb(100,200,255)

; use DBpro functions to grab the R,G,B levels from this colour value
Print RgbR(C)
Print RgbG(C)
Print RgbB(C)

; This can done manually using AND and shifting
print (C and $ff0000) >> 16
print (C and $ff00) >> 8
print (C and $ff)



Example

or perhaps you want to change the R level without having to break the colour down to separate R,G,B values, only to then send it back through the RGB() function again.

do to that you'd use the AND operation to mask off the RED channel (zero it's bits) from your original colour (so retaining Green and BLUE), then shift your new RED value into position and OR it on.

So this

NewColour =Rgb(NewR, RgbG(OldColour),RgbB(OldColuor))


Could be done like this.

NewColour = (OldColour && $00ffff) || (NewR <<16)



Example

In this example were going to halve the brightness of a colour. To do, all we do is Shift the bits in the colour right 1 position. Then mask off the high bits of R,G,B channels, to the leave the dulled colour.


So this,

NewColour = rgb(RgbR(OldColour)/2,RgbG(OldColour)/2,RgbB(OldColour)/2)


Can also be done like this

NewColour =(OldColour>>1) and $7f7f7f

Mattman
22
Years of Service
User Offline
Joined: 5th Jun 2003
Location: East Lansing
Posted: 24th Jan 2006 00:15 Edited at: 24th Jan 2006 00:16
That was one of the tutorials I was going to put up (well something like that). Will have more later, like I said this was a quickie.

Testing Odd or Even using AND
2 && 1 or 10 && 01 will result in 00
3 && 1 or 11 && 01 will result in 01
The premise behind this is that the last digit in every odd number will be 1 and it will be 0 for even numbers.

function IsOdd(a as integer)
local returnvalue as boolean
returnvalue = a && 1
endfunction returnvalue

Will return 1 if the number is odd, or 0 if it is Even.

PS will be back later showing how to invert the colors of a picture (hint hint, XOR inverts data...)

Why make sense when you could make brownies?

Login to post a reply

Server time is: 2025-06-06 17:37:06
Your offset time is: 2025-06-06 17:37:06