BitClearString, BitFlipString, and BitSetString (String functions): Difference between revisions
m (Admin moved page BitSetString (String function) to BitClearString, BitFlipString, and BitSetString (String function)) |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{Template:String:BitSetString subtitle}} | {{Template:String:BitSetString subtitle}} | ||
These functions provide the ability to manipulate individual bits in a string via a bit number. | |||
==Syntax== | ==Syntax== | ||
{{Template:String:BitClearString syntax}} | |||
{{Template:String:BitFlipString syntax}} | |||
{{Template:String:BitSetString syntax}} | {{Template:String:BitSetString syntax}} | ||
===Syntax terms=== | ===Syntax terms=== | ||
<table class="syntaxTable"> | <table class="syntaxTable"> | ||
<tr><th>%result</th><td>string</td></tr> | <tr><th>%result</th><td>A copy of the input (method object) string with a single bit (possibly) modified.</td></tr> | ||
<tr><th>string</th> | <tr><th>string</th> | ||
<td>string</td></tr> | <td>The string for which a bit is to be modified.</td></tr> | ||
<tr><th>bitNumber</th> | <tr><th>bitNumber</th> | ||
<td>number</td></tr> | <td>The bit number in the input string that is to be modified. Note that this is a bit number not a bit offset. So the first bit in the string is bit number 1.</td></tr> | ||
</table> | </table> | ||
<ul> | |||
<li><var>BitClearString</var> turns off the indicated bit. If it is already off, the bit stays off.</li> | |||
<li><var>BitFlipString</var> changes the indicated bit. If it is on, the bit is turned off; if it is off, the bit is turned on.</li> | |||
<li><var>BitSetString</var> turns on the indicated bit. If it is already on, the bit stays on.</li> | |||
</ul> | |||
==Usage notes== | ==Usage notes== | ||
<ul> | |||
<li>Requesting a non-positive bit number, or a bit number past the the end of the string results in an [[InvalidBitNumber class|InvalidBitNumber exception]] being thrown.</li> | |||
<li>Since each byte in a string has 8 bits, the valid bit numbers range from 1 to the length of the string times 8. So a null (zero-length) string has no valid bits.</li> | |||
<li>The value of a bit in a string can be tested with the [[BitValueString (String function)|BitValueString function]].</li> | |||
<li>Manipulating bits in a string provides an CPU and space efficient alternative to an array or [Arraylist Class|Arraylist]] when the possible values are always 0 or 1 or <var>True</var> or <var>False</var>. However, because the bit manipulation methods require the copying of the string value for each operation, this benefit tends to be blunted as the strings get longer. So while there is no restriction on the length of a string and bit number with the string that these methods will update – they can even update bit numbers greater than 2G since a 2G-1 longstring has 16G-8 bits</li> – it's probably not a good idea to use these functions heavily on strings longer than 255 bytes (2040 bits) or so.</li> | |||
</ul> | |||
==Examples== | ==Examples== | ||
This example creates a 128-byte string of null bytes, then sets every 3rd bit, clears every 4th bit, and then flips (inverts), every 5th bit, finally display the results in hexadecimal format: | |||
<p class="code">%string is string len 255 | |||
%i is float | |||
... | |||
%string = '00':x:[[Left (String function)|left]](128, pad='00':x') | |||
for %i from 1 to %string:length * 8 by 3 | |||
%string = %string:bitSetString(%i) | |||
end for | |||
for %i from 1 to %string:length * 8 by 4 | |||
%string = %string:bitClearString(%i) | |||
end for | |||
for %i from 1 to %string:length * 8 by 5 | |||
%string = %string:bitFlipString(%i) | |||
end for | |||
</p> | |||
==See also== | ==See also== | ||
<table> | |||
<tr> | |||
<td> | |||
<ul> | |||
<li><var>[[BitAndString (String function)|BitAndString]]</var> | |||
<li><var>[[BitOffString (String function)|BitOffString]]</var> | |||
<li><var>[[BitOnString (String function)|BitOnString]]</var> | |||
<li><var>[[BitOrString (String function)|BitOrString]]</var> | |||
<li><var>[[BitXorString (String function)|BitXorString]]</var> | |||
</ul> | |||
</td> | |||
<td> | |||
<ul> | |||
<li><var>[[BitCountString (String function)|BitCountString]]</var> | |||
<li><var>[[BitValueString (String function)|BitValueString]]</var> | |||
</ul> | |||
</td> | |||
</table> | |||
{{Template:String:BitSetString footer}} | {{Template:String:BitSetString footer}} |
Latest revision as of 00:05, 22 March 2013
Set a bit in a string (String class)
[Introduced in Model 204 7.5]
These functions provide the ability to manipulate individual bits in a string via a bit number.
Syntax
%result = string:BitClearString( bitNumber) Throws InvalidBitNumber
%result = string:BitFlipString( bitNumber) Throws InvalidBitNumber
%result = string:BitSetString( bitNumber) Throws InvalidBitNumber
Syntax terms
%result | A copy of the input (method object) string with a single bit (possibly) modified. |
---|---|
string | The string for which a bit is to be modified. |
bitNumber | The bit number in the input string that is to be modified. Note that this is a bit number not a bit offset. So the first bit in the string is bit number 1. |
- BitClearString turns off the indicated bit. If it is already off, the bit stays off.
- BitFlipString changes the indicated bit. If it is on, the bit is turned off; if it is off, the bit is turned on.
- BitSetString turns on the indicated bit. If it is already on, the bit stays on.
Usage notes
- Requesting a non-positive bit number, or a bit number past the the end of the string results in an InvalidBitNumber exception being thrown.
- Since each byte in a string has 8 bits, the valid bit numbers range from 1 to the length of the string times 8. So a null (zero-length) string has no valid bits.
- The value of a bit in a string can be tested with the BitValueString function.
- Manipulating bits in a string provides an CPU and space efficient alternative to an array or [Arraylist Class|Arraylist]] when the possible values are always 0 or 1 or True or False. However, because the bit manipulation methods require the copying of the string value for each operation, this benefit tends to be blunted as the strings get longer. So while there is no restriction on the length of a string and bit number with the string that these methods will update – they can even update bit numbers greater than 2G since a 2G-1 longstring has 16G-8 bits – it's probably not a good idea to use these functions heavily on strings longer than 255 bytes (2040 bits) or so.
Examples
This example creates a 128-byte string of null bytes, then sets every 3rd bit, clears every 4th bit, and then flips (inverts), every 5th bit, finally display the results in hexadecimal format:
%string is string len 255 %i is float ... %string = '00':x:left(128, pad='00':x') for %i from 1 to %string:length * 8 by 3 %string = %string:bitSetString(%i) end for for %i from 1 to %string:length * 8 by 4 %string = %string:bitClearString(%i) end for for %i from 1 to %string:length * 8 by 5 %string = %string:bitFlipString(%i) end for