We help IT Professionals succeed at work.

# bit wise operatins using C

on
Medium Priority
503 Views
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!
Comment
Watch Question

## View Solution Only

Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

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

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

Commented:
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')
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

// 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.

Commented:
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 ?

You will then increase the chances to find a good answer to your question

Commented:

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

grade or reject or comment... do something!!
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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;
}

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 .
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile