Help Understanding some Bitwise Operators?
|
Nyx Alsop
Registered User
Join date: 14 Dec 2008
Posts: 252
|
12-16-2009 10:37
Hi.
Could some one tell me what ~, >>, ^ and << do?
I know there NOT, RIGHT SWITCH, XOR and LEFT SWITCH, but I don't know what that means.
I've searched and searched but all I'm finding is examples in binary, could someone just give me a simple practical exaple of them in use?
Thanks!
|
Bear Jharls
Registered User
Join date: 8 Sep 2009
Posts: 59
|
12-16-2009 11:09
~ NOT
flying = True; If IHitTheGround Then flying = ~flying //.flying is now False IF (~flying) Then IAmOnTheGround
<< SHIFT BITS LEFT (multiplication by powers of 2)
1 << 1 = 2 binary 0001 << 1 = 0010 3 << 2 = 12 binary 0011 << 2 = 1100
>> SHIFT BITS RIGHT (division by powers of 2)
0100 >> 1 = 0010 (4 >> 1 = 2) 0110 >> 1 = 0011 (6 >> 1 = 3)
When bit shifting just be careful to recognise that bits get shifted off the ends.
0001 >> 3 = 0000 0010 >> 3 = 0000 0100 >> 3 = 0000
0011 << 3 = 1000 1001 << 3 = 1000
A practical example of bit shifting for a purpose other than mutiply/divide is when you want to read a stream of bits, one bit at a time say from a disk file when you can only read from file one byte at time:
byte buffer; integer bitcounter = 0;
Function GetBit() { If bitcounter = 0 Then buffer = GetByteFromFile; bitcounter = 8; End If bitcounter = bitcounter -1; return buffer >> bitcounter AND 1; /// and 1 masks off the buffer returning only the value of the right-most bit after the shift }
^ XOR
This is useful for obfuscation (amongst other things) as XOR is reversible.
if you have a string of numbers say 3,1,0,2 and you seed a pseudo-random number generator with say 3 so that it produces a string of numbers 2,3,2,1 (or whatever) then:
3 XOR 2 = 1 1 XOR 3 = 2 0 XOR 2 = 2 2 XOR 1 = 3
Giving you a new string: 1,2,2,3
If you reseed the pseudo-random number generator with the original seed (3) and process the new string with XOR it restores the string to its original values:
1 XOR 2 = 3 2 XOR 3 = 1 2 XOR 2 = 0 3 XOR 1 = 2
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
12-16-2009 12:29
in binary ~10101 == 01010 (swap all the digits for their opposite) 10101 >> 1 == 11010 (add the same number to the front, remove the last) 10101 << 1 = 01010 (remove the first, add zero to the end) in decimal ~X == -(X + 1) == -X - 1 (at least with signed integers) X >> Y == X / (llPow( 2, Y ); (fractions truncated) X << Y == X * (llPow( 2, Y ); (results past min/max int give other results) except for ~ they are really only useful to imagine in binary or as truth tables
^ == True if both bits are different | == True if either bit is true & == True if both bits are true order doesn't matter, result is false if it fails the above conditions. to reverse the outcome, use ~ on the result. compare them in columns.
_____________________
| | . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - 
|
Otterax Avalanche
Registered User
Join date: 16 Nov 2006
Posts: 1
|
12-16-2009 12:35
Bear, watch your use of the tilde (~) versus the shriek (!).
In LSL, zero is logically interpreted to be false, and any non-zero value is logically interpreted to be true. The predefined constants TRUE and FALSE, however, are slightly different:
TRUE is equal to 1, not any non-zero integer. So: !TRUE, the logical-not, is equal to FALSE, which is zero. ~TRUE, the bitwise-not, is the one's complement, which is a reversal of all its bits, and is equal to -2. So, !TRUE is logically false, but ~TRUE is still logically interpreted to be true.
This is why I advise scripters to use TRUE and FALSE with direct assignment or to modify them with the logical operators, but to avoid these constants entirely in expressions and mechanisms using arithmetic or bitwise operations.
|
Bear Jharls
Registered User
Join date: 8 Sep 2009
Posts: 59
|
12-16-2009 13:45
From: Otterax Avalanche Bear, watch your use of the tilde (~) versus the shriek (!). Yes True. Each language has its own peculiarities.
|
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
|
12-16-2009 14:10
From: Nyx Alsop I've searched and searched but all I'm finding is examples in binary, could someone just give me a simple practical exaple of them in use? Learn binary!!! Really.. This stuff will make more sense if you do. That's why the examples are in binary!
_____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!! - Go here: http://jira.secondlife.com/browse/SVC-1224- If you see "if you were logged in.." on the left, click it and log in - Click the "Vote for it" link on the left
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
12-16-2009 15:54
it may help to think of binary as a string of 32 true/false variables, with the fun property that each true is multiplied by a power of 2 (the first being power 0) and then added to get a number.
_____________________
| | . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - 
|
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
|
12-16-2009 16:30
From: Void Singer Join Date: Sep 2005 Location: Here, mostly. Posts: 6,009
A slightly belated happy 6k to you!
_____________________
Sick of sims locking up every time somebody TPs in? Vote for SVC-3895!!! - Go here: https://jira.secondlife.com/browse/SVC-3895- If you see "if you were logged in.." on the left, click it and log in - Click the "Vote for it" link on the left
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
12-16-2009 16:43
woo hoo, only 133 more posts to binary 6k, ty =)
_____________________
| | . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - 
|
Nyx Alsop
Registered User
Join date: 14 Dec 2008
Posts: 252
|
12-16-2009 17:51
Know any resource on the web I can learn the Binary needed?
|
Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
|
12-16-2009 18:07
From: Nyx Alsop Know any resource on the web I can learn the Binary needed? Picture an odometer - the thing that tracks how many miles (or km) your car has gone. It starts at all 0's. Move forward - add to the milage - and the rightmost digit progresses through all the numbers. When the rightmost digit goes past 9, it rolls on back to 0 and the digit to the left goes up one. If it, the digit to the left, is also at 9, it rolls back to 0 and the digit to its left goes up one. Etc.. Binary is just a different numbering system. Instead of getting the numbers 0-9, it just gets 0 and 1. So.. Counting to 5 in binary goes: 0001 0010 0011 0100 0101 Hexadecimal (0-9 plus A-F) and octal (0-7) are also just other numbering systems. It's all the same numbers but the way they read out - the characters you use to represent some quantity - is different. Because binary is just 0's and 1's, there's some extra math things you can do that don't make as much sense in other systems. Like ~, the "not" operator, flips all the 0's to 1's and all the 1's to 0's. An OR operation takes two numbers and, for each digit, results in a 1 if either value is 1 and a 0 if both are 0.. So: 0100 OR 1110 = 1110 Similar, an AND operator returns a 1 if *both* digits are 1 and a 0 if either are 0: 0100 AND 1110 = 0100 Read the above a few times, if needed. Once it finally clicks into place, you'll laugh that you didn't get it before. Really - everybody does.  After that, you can hopefully read more on what others talk about above and it will make a bit more sense. It's just numbers and math..
_____________________
Sick of sims locking up every time somebody TPs in? Vote for SVC-3895!!! - Go here: https://jira.secondlife.com/browse/SVC-3895- If you see "if you were logged in.." on the left, click it and log in - Click the "Vote for it" link on the left
|
SuezanneC Baskerville
Forums Rock!
Join date: 22 Dec 2003
Posts: 14,229
|
12-16-2009 18:14
_____________________
-
So long to these forums, the vBulletin forums that used to be at forums.secondlife.com. I will miss them.
I can be found on the web by searching for "SuezanneC Baskerville", or go to
http://www.google.com/profiles/suezanne
-
http://lindenlab.tribe.net/ created on 11/19/03.
Members: Ben, Catherine, Colin, Cory, Dan, Doug, Jim, Philip, Phoenix, Richard, Robin, and Ryan
-
|