Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Tensorflow bitwise.bitwise_and() method

Improve Article
Save Article
  • Last Updated : 04 Jun, 2020
Improve Article
Save Article

Tensorflow bitwise.bitwise_and() method performs the bitwise_and operation and return those bits set, that are set(1) in both a and b. The operation is done on the representation of a and b.
This method belongs to bitwise module.

Syntax: tf.bitwise.bitwise_and( a, b, name=None)

Arguments

  • a: This must be a Tensor.It should be from the one of the following types: int8, int16, int32, int64, uint8, uint16, uint32, uint64.
  • b: This should also be a Tensor, Type same as a.
  • name: This is optional parameter and this is the name of the operation.

Return: It returns a Tensor having the same type as a and b.

Let’s see this concept with the help of few examples:
Example 1:




# Importing the Tensorflow library 
import tensorflow as tf 
  
# A constant a and b
a = tf.constant(4, dtype = tf.int32)
b = tf.constant(6, dtype = tf.int32)  
  
# Applying the bitwise_and() function 
# storing the result in 'c' 
c = tf.bitwise.bitwise_and(a, b) 
  
# Initiating a Tensorflow session 
with tf.Session() as sess:
    print("Input 1", a)
    print(sess.run(a))
    print("Input 2", b)
    print(sess.run(b))
    print("Output: ", c)
    print(sess.run(c))

Output:

Input 1 Tensor("Const_41:0", shape=(), dtype=int32)
4
Input 2 Tensor("Const_42:0", shape=(), dtype=int32)
6
Output:  Tensor("BitwiseAnd_5:0", shape=(), dtype=int32)
4

Example 2:




# Importing the Tensorflow library 
import tensorflow as tf 
  
# A constant a and b
a = tf.constant([1, 2, 7], dtype = tf.int32)
b = tf.constant([1, 5, 8], dtype = tf.int32)  
  
# Applying the bitwise_and() function 
# storing the result in 'c' 
c = tf.bitwise.bitwise_and(a, b) 
  
# Initiating a Tensorflow session 
with tf.Session() as sess:
    print("Input 1", a)
    print(sess.run(a))
    print("Input 2", b)
    print(sess.run(b))
    print("Output: ", c)
    print(sess.run(c))

Output:

Input 1 Tensor("Const_43:0", shape=(3, ), dtype=int32)
[1 2 7]
Input 2 Tensor("Const_44:0", shape=(3, ), dtype=int32)
[1 5 8]
Output:  Tensor("BitwiseAnd_6:0", shape=(3, ), dtype=int32)
[1 0 0]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!