Bitwise operations: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 210: | Line 210: | ||
public section. |
public section. |
||
class-methods: |
class-methods: |
||
logical_shift_left |
|||
importing |
|||
i_value type x |
|||
i_position type x |
|||
exporting |
|||
e_value type x, |
|||
logical_shift_right |
|||
importing |
|||
i_value type x |
|||
i_position type x |
|||
exporting |
|||
e_value type x, |
|||
arithmetic_shift_left |
arithmetic_shift_left |
||
importing |
importing |
||
Line 252: | Line 266: | ||
e_value = i_value div 2 ** i_position. |
e_value = i_value div 2 ** i_position. |
||
endmethod. |
|||
method logical_shift_left. |
|||
clear e_value. |
|||
data(bits) = hex_converter=>to_binary( i_value ). |
|||
data(length_of_bit_sequence) = strlen( bits ). |
|||
bits = shift_left( |
|||
val = bits |
|||
places = i_position ). |
|||
while strlen( bits ) < length_of_bit_sequence. |
|||
bits = bits && `0`. |
|||
endwhile. |
|||
do strlen( bits ) times. |
|||
data(index) = sy-index - 1. |
|||
data(current_bit) = bits+index(1). |
|||
if current_bit eq `1`. |
|||
set bit sy-index of e_value. |
|||
endif. |
|||
enddo. |
|||
endmethod. |
|||
method logical_shift_right. |
|||
clear e_value. |
|||
data(bits) = hex_converter=>to_binary( i_value ). |
|||
data(length_of_bit_sequence) = strlen( bits ). |
|||
bits = shift_right( |
|||
val = bits |
|||
places = i_position ). |
|||
while strlen( bits ) < length_of_bit_sequence. |
|||
bits = `0` && bits. |
|||
endwhile. |
|||
do strlen( bits ) times. |
|||
data(index) = sy-index - 1. |
|||
data(current_bit) = bits+index(1). |
|||
if current_bit eq `1`. |
|||
set bit sy-index of e_value. |
|||
endif. |
|||
enddo. |
|||
endmethod. |
endmethod. |
||
Line 304: | Line 372: | ||
result type x length 4. |
result type x length 4. |
||
write: |a -> { a }, { hex_converter=> |
write: |a -> { a }, { hex_converter=>to_binary( a ) }, { hex_converter=>to_decimal( a ) }|, /. |
||
write: |b -> { b }, { hex_converter=> |
write: |b -> { b }, { hex_converter=>to_binary( b ) }, { hex_converter=>to_decimal( b ) }|, /. |
||
result = a bit-and b. |
result = a bit-and b. |
||
write: |a & b -> { result }, { hex_converter=> |
write: |a & b -> { result }, { hex_converter=>to_binary( result ) }, { hex_converter=>to_decimal( result ) }|, /. |
||
result = a bit-or b. |
result = a bit-or b. |
||
write: |a \| b -> { result }, { hex_converter=> |
write: |a \| b -> { result }, { hex_converter=>to_binary( result ) }, { hex_converter=>to_decimal( result ) }|, /. |
||
result = a bit-xor b. |
result = a bit-xor b. |
||
write: |a ^ b -> { result }, { hex_converter=> |
write: |a ^ b -> { result }, { hex_converter=>to_binary( result ) }, { hex_converter=>to_decimal( result ) }|, /. |
||
result = bit-not a. |
result = bit-not a. |
||
write: |~a -> { result }, { hex_converter=> |
write: |~a -> { result }, { hex_converter=>to_binary( result ) }, { hex_converter=>to_decimal( result ) }|, /. |
||
missing_bitwise_operations=>arithmetic_shift_left( |
missing_bitwise_operations=>arithmetic_shift_left( |
||
exporting |
exporting |
||
i_value = a |
i_value = bit-not a |
||
i_position = b |
i_position = b |
||
importing |
importing |
||
e_value = result ). |
e_value = result ). |
||
write: |a << b -> { result }, { hex_converter=> |
write: |~a << b -> { result }, { hex_converter=>to_binary( result ) }, { hex_converter=>to_decimal( result ) }|, /. |
||
missing_bitwise_operations=>arithmetic_shift_right( |
missing_bitwise_operations=>arithmetic_shift_right( |
||
exporting |
exporting |
||
i_value = a |
i_value = bit-not a |
||
i_position = b |
i_position = b |
||
importing |
importing |
||
e_value = result ). |
e_value = result ). |
||
write: |a >> b -> { result }, { hex_converter=> |
write: |~a >> b -> { result }, { hex_converter=>to_binary( result ) }, { hex_converter=>to_decimal( result ) }|, /. |
||
missing_bitwise_operations=> |
missing_bitwise_operations=>logical_shift_left( |
||
exporting |
exporting |
||
i_value |
i_value = a |
||
i_position = b |
i_position = b |
||
importing |
importing |
||
e_value = result ). |
e_value = result ). |
||
write: | |
write: |a <<< b -> { result }, { hex_converter=>to_binary( result ) }, { hex_converter=>to_decimal( result ) }|, /. |
||
missing_bitwise_operations=> |
missing_bitwise_operations=>logical_shift_right( |
||
exporting |
exporting |
||
i_value |
i_value = a |
||
i_position = b |
i_position = b |
||
importing |
importing |
||
e_value = result ). |
e_value = result ). |
||
write: | |
write: |a >>> b -> { result }, { hex_converter=>to_binary( result ) }, { hex_converter=>to_decimal( result ) }|, /. |
||
missing_bitwise_operations=>rotate_left( |
missing_bitwise_operations=>rotate_left( |
||
Line 358: | Line 426: | ||
importing |
importing |
||
e_value = result ). |
e_value = result ). |
||
write: |~a rotl b -> { result }, { hex_converter=> |
write: |~a rotl b -> { result }, { hex_converter=>to_binary( result ) }, { hex_converter=>to_decimal( result ) }|, /. |
||
missing_bitwise_operations=>rotate_right( |
missing_bitwise_operations=>rotate_right( |
||
Line 366: | Line 434: | ||
importing |
importing |
||
e_value = result ). |
e_value = result ). |
||
write: |a rotr b -> { result }, { hex_converter=> |
write: |a rotr b -> { result }, { hex_converter=>to_binary( result ) }, { hex_converter=>to_decimal( result ) }|, /. |
||
</lang> |
</lang> |
||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
a -> 000000FF, 00000000000000000000000011111111, 255 |
|||
a -> 000000FF, 255, 00000000000000000000000011111111 |
|||
b -> 00000002, 00000000000000000000000000000010, 2 |
|||
b -> 00000002, 2, 00000000000000000000000000000010 |
|||
a & b -> 00000002, |
a & b -> 00000002, 00000000000000000000000000000010, 2 |
||
a b -> 000000FF, |
a b -> 000000FF, 00000000000000000000000011111111, 255 |
||
a ^ b -> 000000FD, |
a ^ b -> 000000FD, 00000000000000000000000011111101, 253 |
||
~a -> FFFFFF00, |
~a -> FFFFFF00, 11111111111111111111111100000000, -256 |
||
~a << b -> FFFFFC00, 11111111111111111111110000000000, -1024 |
|||
a << b -> 000003FC, 1020, 00000000000000000000001111111100 |
|||
~a >> b -> FFFFFFC0, 11111111111111111111111111000000, -64 |
|||
a >> b -> 0000003F, 63, 00000000000000000000000000111111 |
|||
a <<< b -> 000003FC, 00000000000000000000001111111100, 1020 |
|||
~a <<< b -> FFFFFC00, -1024, 11111111111111111111110000000000 |
|||
a >>> b -> 0000003F, 00000000000000000000000000111111, 63 |
|||
~a >>> b -> FFFFFFC0, -64, 11111111111111111111111111000000 |
|||
~a rotl b -> FFFFFC03, |
~a rotl b -> FFFFFC03, 11111111111111111111110000000011, -1021 |
||
a rotr b -> C000003F, |
a rotr b -> C000003F, 11000000000000000000000000111111, -1073741761 |
||
</pre> |
</pre> |
||