1function C = bitget (A, B, assumedtype)
2%BITGET get bit.
3% C = bitget (A,B) returns the value of the bit at position B in A, where
4% A is an integer array.  If B(i,j) is an integer in the range 1 (the
5% least significant bit) to the number of bits in the data type of A, then
6% C(i,j) is that bit of A(i,j).  If B(i,j) is outside this range, C(i,j)
7% is zero; note that this behavior is an extension to the built-in MATLAB
8% bitget, which results in an error for this case.  This modified rule
9% allows the inputs A and B to be sparse.  If B(i,j) is implicitly zero
10% (not in the pattern of B), or if A(i,j) is implicitly zero, then C(i,j)
11% is not an entry in the pattern of C.
12%
13% If A and B are matrices, the pattern of C is the set intersection of A
14% and B.  If one of A or B is a nonzero scalar, the scalar is expanded
15% into a sparse matrix with the same pattern as the other matrix, and the
16% result is a sparse matrix.
17%
18% With a third parameter, C = bitget (A,B,assumedtype) provides a data
19% type to convert A to if it has a floating-point type.  If A already has
20% an integer type, then it is not modified.  Otherwise, A is converted to
21% assumedtype, which can be 'int8', 'int16', 'int32', 'int64', 'uint8',
22% 'uint16', 'uint32' or 'uint64'.  The default is 'uint64'.
23%
24% Example:
25%
26%   A = GrB (magic (4)'*137, 'uint16')
27%   B = GrB (magic (4))
28%   C = bitget (A, B)
29%   fprintf ('\nA: ') ; fprintf ('%3x ', A) ; fprintf ('\n') ;
30%   fprintf ('\nB: ') ; fprintf ('%3x ', B) ; fprintf ('\n') ;
31%   fprintf ('\nC: ') ; fprintf ('%3x ', C) ; fprintf ('\n') ;
32%   % in MATLAB:
33%   C2 = bitget (uint16 (A), uint16 (B))
34%   isequal (C2, C)
35%
36% See also GrB/bitor, GrB/bitand, GrB/bitxor, GrB/bitcmp, GrB/bitshift,
37% GrB/bitset, GrB/bitclr.
38
39% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
40% SPDX-License-Identifier: GPL-3.0-or-later
41
42if (nargin < 3)
43    assumedtype = 'uint64' ;
44end
45
46if (isobject (A))
47    A = A.opaque ;
48end
49
50if (isobject (B))
51    B = B.opaque ;
52end
53
54atype = gbtype (A) ;
55btype = gbtype (B) ;
56
57if (contains (atype, 'complex') || contains (btype, 'complex'))
58    error ('inputs must be real') ;
59end
60
61if (isequal (atype, 'logical') || isequal (btype, 'logical'))
62    error ('inputs must not be logical') ;
63end
64
65if (~contains (assumedtype, 'int'))
66    error ('assumedtype must be an integer type') ;
67end
68
69% C will have the same type as A on input
70ctype = atype ;
71
72% determine the type of A
73if (isequal (atype, 'double') || isequal (atype, 'single'))
74    A = gbnew (A, assumedtype) ;
75    atype = assumedtype ;
76end
77
78% ensure B has the right type
79if (~isequal (btype, atype))
80    B = gbnew (B, atype) ;
81end
82
83% extract the bits from each entry of A
84C = GrB (gb_emult (A, ['bitget.' atype], B), ctype) ;
85
86