跳轉到內容

軟體工程師手冊/語言詞典/PLI/位串

來自華夏公益教科書,開放的書籍,開放的世界

注意:本文需要了解 PL/I 的基本知識,可以在 軟體工程師手冊/語言詞典/PLI 中找到。

位串“作為字串”

[編輯 | 編輯原始碼]

在 PL/I 中,位串形式上被宣告為“1 位字母”的字串,也就是說,所有內建字串函式都可以用於它,

dcl   my_chars   char (8) varying   init ( 'ABCDCD' );
dcl   my_bits    bit  (8) varying   init ( '01010'B );
   put skip list ( length ( my_chars ) );   /* output is 6, the current length of the varying string */
   put skip list ( length ( my_bits  ) );   /* output is 5, the current length of the varying string */
   put skip list ( index ( my_chars , 'CD'  ) );   /* output is 3, the first position of substring 'CD'  */
   put skip list ( index ( my_bits  , '10'B ) );   /* output is 2, the first position of substring '10'B */

位串作為布林值

[編輯 | 編輯原始碼]

位串的常見用法是將它們用作位向量,尤其是位 (1) 串表示單個布林值

  • '1'B 被解釋為真
  • '0'B 被解釋為假

位 (1) 串可以在條件語句和迴圈語句中用作布林表示式。

dcl   one_bit   bit (1);
   one_bit = ( 1 < 2 );   /* now one_bit has the value '1'B */
   one_bit = ( 2 < 1 );   /* now one_bit has the value '0'B */
   if one_bit then
      put skip list ( 'value of one_bit is true' );
   else
      put skip list ( 'value of one_bit is false' );
   do while ( one_bit );
      .....
   end;

在期望位 (1) 值的表示式中使用的位串被解釋為 '1'B = 真,當且僅當至少一位的值為 '1'B。

dcl   many_bits   bit (99);
   if many_bits then
      put skip list ( 'at least one of the bits has value 1'B' );
   else
      put skip list ( 'none of the bits has value 1'B' );
   do while ( many_bits );   /* do while at least one bit is set */
      .....
   end;

基本布林運算子

[編輯 | 編輯原始碼]

布林運算子可用於計算新的位 (1) 值

  • 字首運算子 ¬ 用作邏輯非。
  • 中綴運算子 & 用作邏輯與。
  • 中綴運算子 | 用作邏輯或。
dcl   bit_a    bit (1);
dcl   bit_b    bit (1);
dcl   result   bit (1);
   result = ¬ bit_a;           /* result = '1'B if and only if bit_a is '0'B */
   result =   bit_a & bit_b;   /* result = '1'B if and only if both bit_a and bit_b are '1'B */
   result =   bit_a | bit_b;   /* result = '0'B if and only if both bit_a and bit_b are '0'B */

注意:使用編譯時選項,非運算子和或運算子可以用其他符號替換,
為了與現有的 PL/I 程式相容,通常 ^ 用作非,! 用作或。
注意:在Enterprise PL/I for z/OS 中,¬ 也可以用作中綴運算子,A ¬ B 表示 A XOR B(異或)。

布林運算子也可以用於位 (n) 串,其中 n > 1,
在這種情況下,計算是以逐位方式進行的,即

  • ( A & B ) 的第 1 位 = ( A 的第 1 位 ) & ( B 的第 1 位 )
  • ( A & B ) 的第 2 位 = ( A 的第 2 位 ) & ( B 的第 2 位 )
  • 等等...

如果 A 和 B 的長度不同,則較短的那個會在右側用 '0'B 填充。

dcl   bit_a   bit (3)   init ( '101'B  );
dcl   bit_b   bit (4)   init ( '1100'B  );
   put skip list ( ¬ bit_a );         /* '010'B  */
   put skip list ( ¬ bit_b );         /* '0011'B */
   put skip list ( bit_a & bit_b );   /* '1000'B */
   put skip list ( bit_a | bit_b );   /* '1110'B */

內建函式 BOOL

[編輯 | 編輯原始碼]

所有 16 種可能的二進位制布林運算都可以透過 BOOL 函式完成。

BOOL ( A , B , pattern_4 )

其中 A 和 B 是位串,pattern_4 是位 (4) 串。

讓我們首先假設 A 和 B 都是位 (1),那麼 pattern_4 的函式是

  • pattern_4 的第 1 位定義了 bool 的結果,如果 A = '0'B 且 B = '0'B
  • pattern_4 的第 2 位定義了 bool 的結果,如果 A = '0'B 且 B = '1'B
  • pattern_4 的第 3 位定義了 bool 的結果,如果 A = '1'B 且 B = '0'B
  • pattern_4 的第 4 位定義了 bool 的結果,如果 A = '1'B 且 B = '1'B

如果 A 或 B 是位 (n),其中 n > 1,那麼計算是以逐位方式進行的,如上所述。

pattern_4 的一些可能值

                           alternative      meaning
bool ( A , B , '0001'B )       A  &  B      A AND  B    logical AND
bool ( A , B , '0111'B )       A  |  B      A OR   B    logical OR
bool ( A , B , '0110'B )       A ¬=  B      A XOR  B    exclusive-OR
bool ( A , B , '1110'B )   ¬ ( A  &  B )    A NAND B    NOT AND
bool ( A , B , '1000'B )   ¬ ( A  |  B )    A NOR  B    NOT OR
bool ( A , B , '1001'B )       A  =  B      A IFF  B    equivalence
bool ( A , B , '1101'B )      ¬A  |  B      A  ->  B    implication: if A then B
bool ( A , B , '1011'B )       A  | ¬B      a  <-  B    implication: if B then A

陣列操作

[編輯 | 編輯原始碼]

內建函式 ALL 和 ANY 期望陣列作為引數。

讓我們首先假設引數 ARRAY 是一個位 (1) 陣列,那麼

  • ALL 返回 '1'B = 真,當且僅當 ARRAY 的所有元素都是 '1'B。
  • ANY 返回 '1'B = 真,當且僅當 ARRAY 中至少有 1 個元素是 '1'B。

如果引數 ARRAY 是一個位 (n) 陣列,其中 n > 1,那麼計算是以逐位方式進行的,如上所述。

dcl   array (3)     bit (8)   init ( '11110000'B ,
                                     '11001100'B ,
                                     '10101010'B   );
dcl   number (42)   bin fixed (15);
   put skip list ( ALL ( array ) );   /* output is '10000000'B */
   put skip list ( ANY ( array ) );   /* output is '11111110'B */
   if ANY ( number < 0 ) then         /* expression "number < 0" returns an array of 42 bit (1) strings */
      put skip list ( 'at least 1 number is negative' );
華夏公益教科書