bit wise operatins using C

Posted on 1997-11-19
Last Modified: 2010-04-02
I want an explination of bit wise operations on C,
and some coded examples applyed on it, like multiplying two entered binary numbers and display the output in binary.(using bit-wise oprations)??
Question by:badawi

Accepted Solution

rigansen earned 50 total points
ID: 1256364
At first glance is not possible to multiply two entered binary numbers using direct bitwise operations like :

&  AND
^  XOR (exclusive OR)
|  OR  (inclusive OR)

because the two operators these functions operates with are integers or short, and your binary number entered should be stored like a string (char[]).

I don't know if that's just an example you need or that's really what you want to do !!

I've programmed a lot of times using bitwise operations but it'll be helpful to know what you really need... ok??

so express yourself!!

regards, rigansen.

Expert Comment

ID: 1256365
Sorry rigansen, but your answer doesn't sound very meaningful - and, BTW, shouldn't it have been a comment?

It _is_ possible to multiply two numbers with bitwise operators, unless a bit strage; indeed, that's what the ALU does.

And, you don't need to store a binary as a char[], since, internally, any number is already a binary; but, here, i must have misunderstood you.

Rgds, julio

Expert Comment

ID: 1256366
I think what badawi meant was bit shifting using the << and >> operator for multiplication and division...
But these things work only with a power of 2 so....

Well, could you clarify a bit...


Expert Comment

ID: 1256367
As mentioned by me before, there are three bitwise operators:

The bitwise AND operator (&) that returns the bitwise AND of the two operands. All bits that are on (1) in both the left and right operand are on in the result; bits that are off (0) in either the left or the right operand are off in the result.

The bitwise exclusive OR operator (^) returns the bitwise exclusive OR of the two operands. All bits that are on (1) in either the left or right operand, but not both, are on in the result. Bits that are the same (either on or off) in both operands are off in the result.

The bitwise inclusive OR operator (|) returns the bitwise inclusive OR of the two operands. All bits that are on (1) in either the left or right operand are on in the result. Bits that are off (0) in both operands are off in the result.

Both operands to these bitwise operators must be of integral types.

Also, as mentioned by cph, there are bitwise shift operators:

<< left-shift operator : shifts left all the bits of the operand.

use : operand << number-of-bits-to-shift

2  << 1  is like  0010 << 1  equals  0100 = 4 decimal
10 << 2  is like  1010 << 2  equals  1000 = 8 decimal

>> right-shift operator : shifts right all the bits of the operand.

use : operand >> number-of-bits-to-shift

4 >> 1  is like  0100 >> 1  equals  0010 = 2 decimal
8 >> 3  is like  1000 >> 2  equals  0001 = 1 decimal
2 >> 3  is like  0010 >> 3  equals  0000 = 0 decimal

as you can realize, is possible to use these bitwise shift operators to multiply or divide by a power of 2, wich is faster than normal multiplication or division.

I hope you find this code useful.

#include <stdio.h>
#include <string.h>

int bin2int(char *p)
   int res = 0, len = strlen(p), i, mask=1;
   i = len-1;
   do {
      if (*(p+i)=='1')
         res += mask;
      mask <<= 1; // right-shift the mask in order to access next power of 2
   } while (i--);
   return res;

void main()
   // internal representation of the binary numbers to multiply.
   char *b1 = "0110",
        *b2 = "0111";
   int i,
      bin1, bin2, // variables to hold the binary digits
      reg = 0, // temporal register
      tmp, // temporal variable
      mask = 1; // mask to access the LSB

   // bin to int conversion in order to accomplish bitwise operations
   bin1 = bin2int(b1);
   bin2 = bin2int(b2);

   // calculate bin1 times bin2
   do {
      // common add, do you need a bitwise operation here also ??  :)
      tmp = reg + bin1;      
      bin1 <<= 1;   // left-shift bin1
      if (mask & bin2) // if LSB digit of bin2 is 1...
         reg = tmp;   // ... update the temporal register.
      bin2 >>= 1;   // right-shift bin2
   } while (bin2); // when bin2 reaches zero, nothing more to multiply...

   i = 8;
   mask <<= i;   // starts displaying from the MSB

   // display the result as a binary digit
   printf("res : ");
   do {
      tmp = reg & mask;      // get the i-th bit
      tmp >>= i;   // access the bit as a LSB
      printf("%d", tmp);
      mask >>= 1; // right-shift the mask in order to access next bit
   } while (i--);


regards, rigansen.
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.


Expert Comment

ID: 1256368
The question has many points.
You want an explanation of the algorithms used to do operations like multiplication, division in binary ?
You want implementations in C as examples ?
You want to output values in binary format ?

Could you please rexpress your question in a clearer way ?
You will then increase the chances to find a good answer to your question


Expert Comment

ID: 1256369
hey badawi, wake up!!

everybody but you seems to be interested in your question!!

grade or reject or comment... do something!!
LVL 84

Expert Comment

ID: 1256370
int add(int a,int b){
        while( b ){
              int c;
                c = a&b;
                a ^= b;
                b = c<<1;
        return a;
int mul(int a,unsigned int b){
        int m;
        m = 0;
        while( b ){
                if( b&1 ){ m = add(m,a); }
                b >>= 1;
                a <<= 1;
        return m;


Author Comment

ID: 1256371
    Thankyou for the answer that just what I wanted ,and you covered it all. And I'm sorry couldn't reply you fast i was so busy .

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now