• C

bit wise operatins using C

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!
badawiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rigansenCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
julio011597Commented:
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
cphCommented:
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
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

rigansenCommented:
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
meessenCommented:
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
rigansenCommented:
hey badawi, wake up!!

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

grade or reject or comment... do something!!
0
ozoCommented:
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
badawiAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.