Digital Logic AND Gate with C Bitwise Operations

Digital Logic AND Gate with C Bitwise Operations:


AND Logic:

A, B is input, Q is output,

A . B = Q  /* AND is represented by dot ( . ) */

AND Truth Table,

A  B     Q (A . B)
==========
0  0     0
0  1     0
1  0     0
1  1     1

C Code:
In C, C++ AND bit wise AND can be performed using &( Ampersand )

Don’t confuse single logical AND ( Double Ampersand && ) with bitwise AND ( Single Ampersand & ).

printf("%d\n", 5 & 6 );    /* Output 4  */
printf("%d\n", 8 & 4 );    /* Output 0  */
printf("%d\n", 54 & 29);   /* Output 20 */

Code Explanation:

Bit wise AND takes two binary digits of same length and performs AND operation on corresponding bits of first and second number. If both bits are 1 then result is 1, otherwise result is 0. Use the table given above.

5 & 6 = 4, 

0101 # 5        /* 0 + 4 + 0 + 1 = 5 */
0110 # 6        /* 0 + 4 + 2 + 0 = 6 */
=========
0100 # 4        /* 0 + 4 + 0 + 0 = 4 */

8 & 4 = 0, 

1000 # 8        /* 8 + 0 + 0 + 0 = 8 */
0100 # 4        /* 0 + 4 + 0 + 0 = 4 */
=========
0000 # 0        /* 0 + 0 + 0 + 0 = 0 */

54 & 29 = 20, 

0011 0110 # 54  /* 0 + 0 + 32 + 16 + 0 + 4 + 2 + 0 = 54 */
0001 1101 # 29  /* 0 + 0 +  0 + 16 + 8 + 4 + 0 + 1 = 29 */
==============
0001 0100 # 20  /* 0 + 0 +  0 + 16 + 0 + 4 + 0 + 0 = 20 */

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s