Solved

bit wise operatins using C

Posted on 1997-11-19
8
433 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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:rigansen
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:meessen
Comment Utility
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
Comment Utility
hey badawi, wake up!!

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

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

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

743 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

16 Experts available now in Live!

Get 1:1 Help Now