Bitwise operations on integersFebruary 03. 2012
Integers in WirbelWirbel's integer datatype is mapped directly to the C datatype int, which is assumed to be the most efficient integer datatype on the architecture. It is signed and either 32 or 64 bits wide. Bitwise operations on integersAs of Wirbel 0.1.11 there are defined bitwise integer operators as known from C as well as a subscription operator not known in any other language I know (see below). The following operators are defined:
All of those (except ~) are also available as assignment operators:
You'll find example usages of those operators in the source code in the file tests/int_bitwise.w. Subscription operatorsWirbel lets you use an integer a little bit like a list of boolean values with the fixed length of 32 or 64. You can used the square brackets to directly address the bits: a = 0 a[0] = true a[2] = false print(a) This will output 5 since this first bit (value 1) and the third bit (value 4) have been switched on. You can also query arbitrary bits:
if a[4]:
print("Bit 4 is on -> a contains a 16")
Splices won't currently work but may follow in a later release of Wirbel. Performance issuesAll bitwise integers operations are directly mapped to the C pendants and thus are as fast as C can be. The subscription operators are implemented like one would do in C via &, | and ~. |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||