Bitwise operations: Difference between revisions

Content added Content deleted
imported>Acediast
(→‎ISO COBOL: removed deprecated REMARKS paragraph)
imported>Acediast
(→‎{{header|COBOL}}: Added implication and equivalence (this is entirely theoretical; only Micro Focus supports this atm))
Line 2,848: Line 2,848:
;;There is no built-in for rotation.</syntaxhighlight>
;;There is no built-in for rotation.</syntaxhighlight>
=={{header|COBOL}}==
=={{header|COBOL}}==
===ISO COBOL===
{{Works with|COBOL 2023}}
COBOL 2002 added support for bitwise operations. Shift and rotation operators were added in COBOL 2023. Results are displayed in decimal.
COBOL 2002 added support for bitwise operations. Shift and rotation operators were added in COBOL 2023. Results are displayed in decimal.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
Line 2,858: Line 2,858:
01 b PIC 1(32) USAGE BIT.
01 b PIC 1(32) USAGE BIT.
01 result PIC 1(32) USAGE BIT.
01 result PIC 1(32) USAGE BIT.
01 result-disp REDEFINES result PIC S9(9) COMP.
01 result-disp REDEFINES result
PIC S9(9) USAGE COMPUTATIONAL.

LINKAGE SECTION.
LINKAGE SECTION.
01 a-int USAGE BINARY-LONG.
01 a-int USAGE BINARY-LONG.
Line 2,879: Line 2,879:
COMPUTE result = a B-XOR b
COMPUTE result = a B-XOR b
DISPLAY "a exclusive-or b is " result-disp
DISPLAY "a exclusive-or b is " result-disp

*> More complex operations can be constructed from these.

COMPUTE result = B-NOT (a B-XOR b)
DISPLAY "Logical equivalence of a and b is " result

COMPUTE result = (B-NOT a) B-AND b
DISPLAY "Logical implication of a and b is " result


*> Shift and rotation operators were only added in COBOL 2023.
*> Shift and rotation operators were only added in COBOL 2023.
Line 2,898: Line 2,906:
END PROGRAM bitwise-ops.</syntaxhighlight>
END PROGRAM bitwise-ops.</syntaxhighlight>


{{works with|GnuCOBOL}}
===Visual COBOL===
{{works with|Visual COBOL}}
{{works with|Visual COBOL}}
In older implementations, non-standard extensions were developed as built-in subroutines.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. mf-bitwise-ops.
PROGRAM-ID. mf-bitwise-ops.

DATA DIVISION.
DATA DIVISION.
LOCAL-STORAGE SECTION.
LOCAL-STORAGE SECTION.
01 result USAGE BINARY-LONG.
01 result USAGE BINARY-LONG.

78 arg-len VALUE LENGTH OF result.
78 arg-len VALUE LENGTH OF result.

LINKAGE SECTION.
LINKAGE SECTION.
01 a USAGE BINARY-LONG.
01 a USAGE BINARY-LONG.
01 b USAGE BINARY-LONG.
01 b USAGE BINARY-LONG.

PROCEDURE DIVISION USING a, b.
PROCEDURE DIVISION USING a, b.
main-line.
main-line.
Line 2,918: Line 2,926:
CALL "CBL_AND" USING a, result, VALUE arg-len
CALL "CBL_AND" USING a, result, VALUE arg-len
DISPLAY "a and b is " result
DISPLAY "a and b is " result

MOVE b TO result
MOVE b TO result
CALL "CBL_OR" USING a, result, VALUE arg-len
CALL "CBL_OR" USING a, result, VALUE arg-len
DISPLAY "a or b is " result
DISPLAY "a or b is " result

MOVE a TO result
MOVE a TO result
CALL "CBL_NOT" USING result, VALUE arg-len
CALL "CBL_NOT" USING result, VALUE arg-len
DISPLAY "Not a is " result
DISPLAY "Not a is " result

MOVE b TO result
MOVE b TO result
CALL "CBL_XOR" USING a, result, VALUE arg-len
CALL "CBL_XOR" USING a, result, VALUE arg-len
DISPLAY "a exclusive-or b is " result
DISPLAY "a exclusive-or b is " result

MOVE b TO result
MOVE b TO result
CALL "CBL_EQ" USING a, result, VALUE arg-len
CALL "CBL_EQ" USING a, result, VALUE arg-len
DISPLAY "Logical equivalence of a and b is " result
DISPLAY "Logical equivalence of a and b is " result

MOVE b TO result
MOVE b TO result
CALL "CBL_IMP" USING a, result, VALUE arg-len
CALL "CBL_IMP" USING a, result, VALUE arg-len
Line 2,941: Line 2,949:
GOBACK.
GOBACK.


END PROGRAM mf-bitwise-ops.</syntaxhighlight>
END PROGRAM mf-bitwise-ops.</syntaxhighlight>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==