|
41 | 41 |
|
42 | 42 |
|
43 | 43 | import dpctl.tensor._tensor_elementwise_impl as ti
|
| 44 | +import numpy |
44 | 45 |
|
45 | 46 | from dpnp.dpnp_algo.dpnp_elementwise_common import DPNPBinaryFunc, DPNPUnaryFunc
|
46 | 47 |
|
47 | 48 | __all__ = [
|
| 49 | + "binary_repr", |
48 | 50 | "bitwise_and",
|
49 | 51 | "bitwise_invert",
|
50 | 52 | "bitwise_left_shift",
|
|
58 | 60 | ]
|
59 | 61 |
|
60 | 62 |
|
| 63 | +def binary_repr(num, width=None): |
| 64 | + """ |
| 65 | + Return the binary representation of the input number as a string. |
| 66 | +
|
| 67 | + For negative numbers, if `width` is not given, a minus sign is added to the |
| 68 | + front. If `width` is given, the two's complement of the number is returned, |
| 69 | + with respect to that width. |
| 70 | +
|
| 71 | + In a two's-complement system negative numbers are represented by the two's |
| 72 | + complement of the absolute value. A N-bit two's-complement system can |
| 73 | + represent every integer in the range :math:`-2^{N-1}` to :math:`+2^{N-1}-1`. |
| 74 | +
|
| 75 | + For full documentation refer to :obj:`numpy.binary_repr`. |
| 76 | +
|
| 77 | + Parameters |
| 78 | + ---------- |
| 79 | + num : int |
| 80 | + Only an integer decimal number can be used. |
| 81 | + width : {None, int}, optional |
| 82 | + The length of the returned string if `num` is positive, or the length |
| 83 | + of the two's complement if `num` is negative, provided that `width` is |
| 84 | + at least a sufficient number of bits for `num` to be represented in the |
| 85 | + designated form. If the `width` value is insufficient, an error is |
| 86 | + raised. |
| 87 | + Default: ``None``. |
| 88 | +
|
| 89 | + Returns |
| 90 | + ------- |
| 91 | + bin : str |
| 92 | + Binary representation of `num` or two's complement of `num`. |
| 93 | +
|
| 94 | + See Also |
| 95 | + -------- |
| 96 | + :obj:`dpnp.base_repr` : Return a string representation of a number in the |
| 97 | + given base system. |
| 98 | + bin : Python's built-in binary representation generator of an integer. |
| 99 | +
|
| 100 | + Notes |
| 101 | + ----- |
| 102 | + :obj:`dpnp.binary_repr` is equivalent to using :obj:`dpnp.base_repr` with |
| 103 | + base 2, but significantly faster. |
| 104 | +
|
| 105 | + Examples |
| 106 | + -------- |
| 107 | + >>> import numpy as np |
| 108 | + >>> np.binary_repr(3) |
| 109 | + '11' |
| 110 | + >>> np.binary_repr(-3) |
| 111 | + '-11' |
| 112 | + >>> np.binary_repr(3, width=4) |
| 113 | + '0011' |
| 114 | +
|
| 115 | + The two's complement is returned when the input number is negative and |
| 116 | + `width` is specified: |
| 117 | +
|
| 118 | + >>> np.binary_repr(-3, width=3) |
| 119 | + '101' |
| 120 | + >>> np.binary_repr(-3, width=5) |
| 121 | + '11101' |
| 122 | +
|
| 123 | + """ |
| 124 | + |
| 125 | + return numpy.binary_repr(num, width) |
| 126 | + |
| 127 | + |
61 | 128 | _BITWISE_AND_DOCSTRING = """
|
62 | 129 | Computes the bitwise AND of the underlying binary representation of each
|
63 | 130 | element `x1_i` of the input array `x1` with the respective element `x2_i`
|
|
98 | 165 | :obj:`dpnp.logical_and` : Compute the truth value of ``x1`` AND ``x2`` element-wise.
|
99 | 166 | :obj:`dpnp.bitwise_or`: Compute the bit-wise OR of two arrays element-wise.
|
100 | 167 | :obj:`dpnp.bitwise_xor` : Compute the bit-wise XOR of two arrays element-wise.
|
| 168 | +:obj:`dpnp.binary_repr` : Return the binary representation of the input number |
| 169 | + as a string. |
101 | 170 |
|
102 | 171 | Examples
|
103 | 172 | --------
|
104 | 173 | >>> import dpnp as np
|
105 | 174 | >>> x1 = np.array([2, 5, 255])
|
106 |
| ->>> x2 = np.array([3,14,16]) |
| 175 | +>>> x2 = np.array([3, 14, 16]) |
107 | 176 | >>> np.bitwise_and(x1, x2)
|
108 |
| -[2, 4, 16] |
| 177 | +array([ 2, 4, 16]) |
109 | 178 |
|
110 | 179 | >>> a = np.array([True, True])
|
111 | 180 | >>> b = np.array([False, True])
|
|
117 | 186 |
|
118 | 187 | >>> x1 & x2
|
119 | 188 | array([ 2, 4, 16])
|
| 189 | +
|
| 190 | +The number 13 is represented by ``00001101``. Likewise, 17 is represented by |
| 191 | +``00010001``. The bit-wise AND of 13 and 17 is therefore ``000000001``, or 1: |
| 192 | +
|
| 193 | +>>> np.bitwise_and(np.array(13), 17) |
| 194 | +array(1) |
| 195 | +
|
| 196 | +>>> np.bitwise_and(np.array(14), 13) |
| 197 | +array(12) |
| 198 | +>>> np.binary_repr(12) |
| 199 | +'1100' |
| 200 | +>>> np.bitwise_and(np.array([14, 3]), 13) |
| 201 | +array([12, 1]) |
120 | 202 | """
|
121 | 203 |
|
122 | 204 | bitwise_and = DPNPBinaryFunc(
|
|
167 | 249 | :obj:`dpnp.logical_or` : Compute the truth value of ``x1`` OR ``x2`` element-wise.
|
168 | 250 | :obj:`dpnp.bitwise_and`: Compute the bit-wise AND of two arrays element-wise.
|
169 | 251 | :obj:`dpnp.bitwise_xor` : Compute the bit-wise XOR of two arrays element-wise.
|
| 252 | +:obj:`dpnp.binary_repr` : Return the binary representation of the input number |
| 253 | + as a string. |
170 | 254 |
|
171 | 255 | Examples
|
172 | 256 | --------
|
|
181 | 265 |
|
182 | 266 | >>> x1 | x2
|
183 | 267 | array([ 6, 5, 255])
|
| 268 | +
|
| 269 | +The number 13 has the binary representation ``00001101``. Likewise, 16 is |
| 270 | +represented by ``00010000``. The bit-wise OR of 13 and 16 is then ``00011101``, |
| 271 | +or 29: |
| 272 | +
|
| 273 | +>>> np.bitwise_or(np.array(13), 16) |
| 274 | +array(29) |
| 275 | +>>> np.binary_repr(29) |
| 276 | +'11101' |
184 | 277 | """
|
185 | 278 |
|
186 | 279 | bitwise_or = DPNPBinaryFunc(
|
|
231 | 324 | :obj:`dpnp.logical_xor` : Compute the truth value of ``x1`` XOR `x2`, element-wise.
|
232 | 325 | :obj:`dpnp.bitwise_and`: Compute the bit-wise AND of two arrays element-wise.
|
233 | 326 | :obj:`dpnp.bitwise_or` : Compute the bit-wise OR of two arrays element-wise.
|
| 327 | +:obj:`dpnp.binary_repr` : Return the binary representation of the input number |
| 328 | + as a string. |
234 | 329 |
|
235 | 330 | Examples
|
236 | 331 | --------
|
|
250 | 345 |
|
251 | 346 | >>> a ^ b
|
252 | 347 | array([ True, False])
|
| 348 | +
|
| 349 | +The number 13 is represented by ``00001101``. Likewise, 17 is represented by |
| 350 | +``00010001``. The bit-wise XOR of 13 and 17 is therefore ``00011100``, or 28: |
| 351 | +
|
| 352 | +>>> np.bitwise_xor(np.array(13), 17) |
| 353 | +array(28) |
| 354 | +>>> np.binary_repr(28) |
| 355 | +'11100' |
253 | 356 | """
|
254 | 357 |
|
255 | 358 | bitwise_xor = DPNPBinaryFunc(
|
|
298 | 401 | :obj:`dpnp.bitwise_or` : Compute the bit-wise OR of two arrays element-wise.
|
299 | 402 | :obj:`dpnp.bitwise_xor` : Compute the bit-wise XOR of two arrays element-wise.
|
300 | 403 | :obj:`dpnp.logical_not` : Compute the truth value of NOT x element-wise.
|
| 404 | +:obj:`dpnp.binary_repr` : Return the binary representation of the input number |
| 405 | + as a string. |
301 | 406 |
|
302 | 407 | Examples
|
303 | 408 | --------
|
304 | 409 | >>> import dpnp as np
|
| 410 | +
|
| 411 | +The number 13 is represented by ``00001101``. The invert or bit-wise NOT of 13 |
| 412 | +is then: |
| 413 | +
|
305 | 414 | >>> x = np.array([13])
|
306 | 415 | >>> np.invert(x)
|
307 |
| --14 |
| 416 | +array([-14]) |
| 417 | +>>> np.binary_repr(-14, width=8) |
| 418 | +'11110010' |
308 | 419 |
|
309 | 420 | >>> a = np.array([True, False])
|
310 | 421 | >>> np.invert(a)
|
|
315 | 426 |
|
316 | 427 | >>> ~a
|
317 | 428 | array([False, True])
|
| 429 | +
|
318 | 430 | """
|
319 | 431 |
|
320 | 432 | invert = DPNPUnaryFunc(
|
|
368 | 480 | See Also
|
369 | 481 | --------
|
370 | 482 | :obj:`dpnp.right_shift` : Shift the bits of an integer to the right.
|
| 483 | +:obj:`dpnp.binary_repr` : Return the binary representation of the input number |
| 484 | + as a string. |
371 | 485 |
|
372 | 486 | Examples
|
373 | 487 | --------
|
|
382 | 496 |
|
383 | 497 | >>> x1 << x2
|
384 | 498 | array([10, 20, 40])
|
| 499 | +
|
| 500 | +>>> np.binary_repr(5) |
| 501 | +'101' |
| 502 | +>>> np.left_shift(np.array(5), 2) |
| 503 | +array(20) |
| 504 | +>>> np.binary_repr(20) |
| 505 | +'10100' |
385 | 506 | """
|
386 | 507 |
|
387 | 508 | left_shift = DPNPBinaryFunc(
|
|
434 | 555 | See Also
|
435 | 556 | --------
|
436 | 557 | :obj:`dpnp.left_shift` : Shift the bits of an integer to the left.
|
| 558 | +:obj:`dpnp.binary_repr` : Return the binary representation of the input number |
| 559 | + as a string. |
437 | 560 |
|
438 | 561 | Examples
|
439 | 562 | --------
|
|
448 | 571 |
|
449 | 572 | >>> x1 >> x2
|
450 | 573 | array([5, 2, 1])
|
| 574 | +
|
| 575 | +>>> np.binary_repr(10) |
| 576 | +'1010' |
| 577 | +>>> np.right_shift(np.array(10), 1) |
| 578 | +array(5) |
| 579 | +>>> np.binary_repr(5) |
| 580 | +'101' |
451 | 581 | """
|
452 | 582 |
|
453 | 583 | right_shift = DPNPBinaryFunc(
|
|
0 commit comments