Skip to main content


Showing posts from March, 2008

How Bitwise Operators are Used, an Example Program

Well, one-by-one we’ve discussed each of the Bitwise Operator . Starting from Operation on Bits and Bitwise Operators , we moved on to Right/Left Bit Shift Operators then discussed Decimal Number to Binary Conversion Program . and at last One's Complement and XOR Operators . After having so much theoretical it’s time now for a nice Example Program, which is the topic of today’s post. The code here is basically to show how these bitwise operator are used rather than what they are used for. // Example Program to demonstrate how // One's Complement (~) and XOR (^) // Opeartors are used. #include<stdio.h> // prototype void showbits( short int ); // defined void showbits( short int dec_num) { short int loop, bit, and_mask; for (loop= 15 ; loop>= 0 ; loop--) { and_mask= 1 <<loop; bit=dec_num&and_mask; if (bit== 0 ) printf( "0" ); else printf( "1" );

One's Complement and XOR Operators

Talking about Bit Operators we are left with two of them, which we’ll be discussing in this article. One’s Complement Operator (~) It takes and works only on one operand. On taking one’s complement of any variable, the 0s are changed to 1 and vice-versa from the bit structure (binary representation) of that variable. The following example will make it easier to understand: Suppose we have a short int a short int a = 16; its binary representation will be 0000000000010000 (decimal 16) on taking one’s complement like below res = ~a; res will contain 1111111111101111 (decimal 65519) It can be used as a part of algorithm to encrypt data. XOR (eXclusive OR) (^) It is derived from the OR Operator and takes two operands to work on. It compares bits like the OR bitwise operator but exclusively for OR cases. Following will clarify what it does: short int a = 46265, its binary form 1011010010111001 another short int b = 46734, binary 101101101000111

Decimal Number to Binary Conversion Program

Please read Operation on Bits and Bitwise Operators and Right/Left Bit Shift Operators if you haven’t already. This post is based on those articles. We’ll be using the following operators and the respective properties for decimal to binary conversion: AND (&) Operator from the article Operation on Bits and Bitwise Operators : Its property to be able to check whether a particular bit is ON (1) or OFF (0). Left Bit Shift Operator (<<) from the article Right/Left Bit Shift Operators : Its property to shift bits (of byte(s)) to desired number of places to the left. After making you guys familiar with the two things above, the program will be easier to understand. // Example Program to convert decimal number // to binary equivalent. // -------- // Function: We have defined a function 'showbits()' // It shows the bit structure of the // Short Int(2 Bytes) passed as argum

Right/Left Bit Shift Operators

This is the continuation of the article Operation on Bits and Bitwise Operators . If you haven’t read that, it is strongly recommended that you do, before proceeding with this article. Bit shifting, as the name signifies, does shifting of bits in byte(s). There are basically two ways, in which bits (of a byte) can be shifted, either to the right, or to the left. Thus we have two types of bit shifting operator. If you think logically, its pretty clear that for bit shifting in a byte, we need to have two data. We need the byte(s) to shift bits on and the number of bits to be shifted. Guess what, the two operators need these to data as operands! Right Bit Shifting Operator (>>) Syntax: res = var >> num; This would shift all bits in the variable var, num places to the right which would get stored to the variable res . So for example if var has the following bit structure: var = 00110101 (decimal 53) And we do the following operation: res =