Expecco Code Snippets/en
Inhaltsverzeichnis
Code Snippets for Common Tasks[Bearbeiten]
This page provides code snippets for common tasks. You can copy-paste these snippets into the code editor and modify as appropriate. You can also paste them into a Workspace window and execute them there. Finally, you may also find those snippets via the Method Finder, when entering the desired result value.
Bits & Bytes[Bearbeiten]
Set, Clear or Toggle a Bit[Bearbeiten]
Smalltalk:
|numIn result| numIn := 0b10011001. "/ using masks result := numIn bitOr: 0b00110000. "/ to set the two bits result := numIn bitAnd: 0b11110000. "/ to mask bits result := numIn bitClear: 0b00000011. "/ to clear bits result := numIn bitXor: 0b00001100. "/ to toggle bits result := numIn bitTest: 0b00001100. "/ to test if masked bits are all set "/ using bit numbers (index starts with 1 for the least significant bit) result := numIn bitAt: 1. "/ to extract the first bit result := numIn setBit: 2. "/ set the second bit result := numIn clearBit: 2. "/ clear the second bit result := numIn invertBit: 2. "/ toggle the second bit
JavaScript:
var numIn, result; numIn = 0b10011001; // using masks result = numIn | 0b00110000; // to set the two bits result = numIn & 0b11110000; // to mask bits result = numIn & ˜0b00000011; // to clear bits result = numIn ^ 0b00001100; // to toggle bits result = (numIn & 0b00001100) != 0; // to test if masked bits are all set // using bit numbers (index starts with 1 for the least significant bit) result = numIn.bitAt(1); // to extract the first bit result = numIn.setBit(2); // set the second bit result = numIn.clearBit(2); // clear the second bit result = numIn.invertBit(2); // toggle the second bit
Shifting[Bearbeiten]
Smalltalk:
|numIn result| numIn := 0xABCD. result := numIn bitShift: 8. "/ left shift by 8 result := numIn bitShift: -8. "/ right shift by 8 result := numIn << 8. "/ alternative left shift result := numIn >> 8. "/ alternative right shift
JavaScript:
var numIn, result; numIn = 0xABCD; result = numIn << 8; // left shift by 8 result = numIn >> 8; // right shift by 8
Extracting Bytes/Ints/Floats from a ByteArray[Bearbeiten]
Typical tasks when receiving binary encoded communication protocol messages are to extract bytes, ints, floats and strings.
Smalltalk:
|bytes result|
"/ constant byte array here; "/ could also come from a file, stream, message or document (see below) bytes := #[ 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 ].
result := bytes at:1. "/ 1 based index here result := bytes[1]. "/ alternative syntax; also 1 based result := bytes signedInt16At:2. "/ signed int16 in machine's native byte order result := bytes signedInt16At:3 MSB:false. "/ in explicit lsb-order result := bytes signedInt16At:3 MSB:true. "/ in explicit msb-order result := bytes signedInt32At:2. "/ same options as above result := bytes signedInt64At:2. "/ same options as above result := bytes signedInt128At:1. "/ same options as above result := bytes unsignedInt16At:2. "/ same for unsigned ints result := bytes unsignedInt16At:3 MSB:false. result := bytes unsignedInt16At:3 MSB:true. result := bytes unsignedInt32At:2. result := bytes unsignedInt64At:2. "/ writing into a byte array bytes int16At:4 put:0x1234. "/ again in native byte order bytes int16At:4 put:0x1234 MSB:true. "/ or explicit order
"/ floats and doubles bytes floatAt:3. "/ extract a 32bit float bytes floatAt:3 MSB:true "/ in given byte order bytes doubleAt:3. "/ extract a 64bit float bytes doubleAt:3 MSB:true "/ in given byte order
"/ strings bytes stringAt:2 size:4. "/ extract bytes as a string bytes zeroByteStringAt:3 maximumSize:3 "/ extract from a 0-terminated C-string