Maxima – 220 – bitwise

ms0

Continuo da qui, copio dal Reference Manual, PDF scaricabile da qui, sono a p.681.

The package bitwise provides functions that allow to manipulate bits of integer constants.
As always maxima attempts to simplify the result of the operation if the actual value of a constant isn’t known considering attributes that might be known for the variables, see the declare mechanism.

bit_not (int)
Inverts all bits of a signed integer. The result of this action reads -int - 1.

(%i1) load("bitwise")$

(%i2) bit_not(bit_not(i));
(%o2)                                  i
(%i3) bit_not(3);
(%o3)                                 - 4
(%i4) bit_not(100);
(%o4)                                - 101
(%i5) bit_not(-101);
(%o5)                                 100

Risulta più evidente in base 2:

$ ion 10 2 100
1100100
$ ion 10 2 -101
-1100101

bit_and (int1, ...)
This function calculates a bitwise and of two or more signed integers.

(%i6) bit_and(i,i);
(%o6)                                  i
(%i7) bit_and(i,i,i);
(%o7)                                  i
(%i8) bit_and(1,3);
(%o8)                                  1
(%i9) bit_and(-7,7);
(%o9)                                  1

If it is known if one of the parameters to bit_and is even this information is taken into consideration by the function.

(%i10) declare(e,even,o,odd);
(%o10)                               done
(%i11) bit_and(1,e);
(%o11)                                 0
(%i12) bit_and(1,o);
(%o12)                                 1

bit_or (int1, ...)
This function calculates a bitwise or of two or more signed integers.

(%i13) bit_or(i,i);
(%o13)                                 i
(%i14) bit_or(i,i,i);
(%o14)                                 i
(%i15) bit_or(1,3);
(%o15)                                 3
(%i16) bit_or(-7,7);
(%o16)                                - 1

If it is known if one of the parameters to bit_or is even this information is taken into consideration by the function.

(%i17) declare(e,even,o,odd);
(%o17)                               done
(%i18) bit_or(1,e);
(%o18)                               e + 1
(%i19) bit_or(1,o);
(%o19)                                 o

bit_xor (int1, ...)
This function calculates a bitwise or of two or more signed integers.

(%i20) bit_xor(i,i);
(%o20)                                 0
(%i21) bit_xor(i,i,i);
(%o21)                                 i
(%i22) bit_xor(1,3);
(%o22)                                 2
(%i23) bit_xor(-7,7);
(%o23)                                - 2

If it is known if one of the parameters to bit_xor is even this information is taken into consideration by the function.

(%i24) declare(e,even,o,odd);
(%o24)                               done
(%i25) bit_xor(1,e);
(%o25)                               e + 1
(%i26) bit_xor(1,o);
(%o26)                               o - 1

bit_lsh (int, nBits)
This function shifts all bits of the signed integer int to the left by nBits bits. The width of the integer is extended by nBits for this process. The result of bit_lsh therefore is int * 2.

(%i27) bit_lsh(0,1);
(%o27)                                 0
(%i28) bit_lsh(1,0);
(%o28)                                 1
(%i29) bit_lsh(1,1);
(%o29)                                 2
(%i30) bit_lsh(1,i);
(%o30)                           bit_lsh(1, i)
(%i31) bit_lsh(-3,1);
(%o31)                                - 6
(%i32) bit_lsh(-2,1);
(%o32)                                - 4

bit_rsh (int, nBits)
This function shifts all bits of the signed integer int to the right by nBits bits. The width of the integer is reduced by nBits for this process.

(%i33) bit_rsh(0,1);
(%o33)                                 0
(%i34) bit_rsh(2,0);
(%o34)                                 2
(%i35) bit_rsh(2,1);
(%o35)                                 1
(%i36) bit_rsh(2,2);
(%o36)                                 0
(%i37) bit_rsh(-3,1);
(%o37)                                - 2
(%i38) bit_rsh(-2,1);
(%o38)                                - 1
(%i39) bit_rsh(-2,2);
(%o39)                                - 1

bit_length (int)
determines how many bits a variable needs to be long in order to store the number int. This function only operates on positive numbers.

(%i40) bit_length(0);
(%o40)                                 0
(%i41) bit_length(1);
(%o41)                                 1
(%i42) bit_length(7);
(%o42)                                 3
(%i43) bit_length(8);
(%o43)                                 4

bit_onep (int, nBit)
determines if bits nBit is set in the signed integer int.

(%i44) bit_onep(85,0);
(%o44)                               true
(%i45) bit_onep(85,1);
(%o45)                               false
(%i46) bit_onep(85,2);
(%o46)                               true
(%i47) bit_onep(85,3);
(%o47)                               false
(%i48) bit_onep(85,100);
(%o48)                               false
(%i49) bit_onep(i,100);
(%o49)                         bit_onep(i, 100)

For signed numbers the sign bit is interpreted to be more than nBit to the left of the leftmost bit of int that reads 1.

(%i50) bit_onep(-2,0);
(%o50)                               false
(%i51) bit_onep(-2,1);
(%o51)                               true
(%i52) bit_onep(-2,2);
(%o52)                               true
(%i53) bit_onep(-2,3);
(%o53)                               true
(%i54) bit_onep(-2,4);
(%o54)                               true

If it is known if the number to be tested is even this information is taken into consideration by the function.

(%i55) declare(e,even,o,odd);
(%o55)                               done
(%i56) bit_onep(e,0);
(%o56)                               false
(%i57) bit_onep(o,0);
(%o57)                               true

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: