Skip to main content

Posts

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 =

Electronics Hobby Shop Launched

You may skip this post if you’re not a resident of India Microcontrollers such as AVR, PIC from Atmel and Microchip are fast becoming the choice of hobbyist for their projects. Now instead of many conventional ICs hobbyists are rather using a single MCU (MicroController Unit) for their projects due to many advantages they have. In the recent years MCUs have become cheap too. Now you may easily get a fully functional MCU from Atmel at under Rs. 70! Sadly that is only one dimension of its popularity if you don’t reside in those big cities, leaving some big cities MCUs are not that easily available let alone the remote areas. There are many resellers but they either deal in large quantities or at high prices both not being suitable for average hobbyists. Seeing this, my brother has come up with a new Online Electronics Hobby Shop for average hobbyists. He has planned to make available everything (Development Boards, MCUs, LCD Displays, Programmers, an