[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

bit wise operatins using C

Posted on 1997-11-19
8
Medium Priority
?
486 Views
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)??
THANKS!
0
Comment
Question by:badawi
8 Comments
 
LVL 2

Accepted Solution

by:
rigansen earned 200 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.
0
 
LVL 5

Expert Comment

by:julio011597
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
0
 
LVL 1

Expert Comment

by:cph
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...

CpH
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:rigansen
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--);

   printf("\n");
}


regards, rigansen.
0
 
LVL 1

Expert Comment

by:meessen
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

0
 
LVL 2

Expert Comment

by:rigansen
ID: 1256369
hey badawi, wake up!!

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

grade or reject or comment... do something!!
0
 
LVL 85

Expert Comment

by:ozo
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;
}

0
 

Author Comment

by:badawi
ID: 1256371
Rigansen,
    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 .
            Badawi,
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

834 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