Click to See Complete Forum and Search --> : How do I do binary arithmetic in c++?
Elijah
11-23-2002, 11:21 PM
I'm supposed to create a crc lookup table for simple non-hex poly's but the way to do it is kinda a dead-end for me. I've searched to some sites for crc algorithms, it only confused me cause of my c++ newbie-ism.
1.)
how do I divide 8-bit binaries to 4-bit poly's (e.g. divide 11010111 by 11011) in c++?
where the arithmetic to do it is:
1+1=0
0+0=0
1+0=1
0+1=1
2.)
another question, since I'll be shifting the width size of the poly to the message:
10110111 + 01001
= 1011011101001
how am I to merge those two?
truls
11-24-2002, 12:48 PM
Is this usefull?
int x = 1;
int y = 2;
// xor = ^
int z = x^y;
printf( "x^y -> z = %d\n", z );
x = 1;
y = 1;
// and = &
z = x & y;
printf( "x&y -> z = %d\n", z );
x = 1;
y = 2;
// or = |
z = x | y;
printf( "x|y -> z = %d\n", z );
x = 1;
y = 1;
// shift y left 2, from 0001(dec:1) to 0100(dec:5)
y = y << 2;
z = x | y;
printf( "x|y -> z = %d\n", z );
Elijah
11-24-2002, 05:06 PM
Wow! :p that could be just what I'm looking for :) .... It's a bit complex for me but I'll try to study it.
Thanks :)
truls
11-24-2002, 06:56 PM
Ok, a little more detail:
and (&) is true (equals 1) of both bits in the same position is 1, otherwise false (equals 0). For example
10010
& 10000
= 10000
xor (^) is true if the bits are unequal (1 and 0, or 0 and 1):
10010
^ 10000
= 00010
or (|) is true if both bits are 1, or if one of the bits is 1:
10010
| 10000
= 10010
The shift left is pretty simple. You simple tell how many places to the left you want to move the bit pattern. So( using a 5 bit pattern):
10010 << 2
= 01000
You have a similar shift right that does the same thing, only, like, to the right :p
So for 1) you would use xor.
For 2) :
10110111 + 01001
= 1011011101001
Since 01001 is 5 bits, you would need to shift the first bit pattern 5 bits to the left, then or (|) the two resulting bit patterns:
0000010110111 << 5
= 1011011100000
1011011100000
| 0000000001001
= 1011011101001
Hope that cleared it up a bit.
Energon
11-24-2002, 08:33 PM
Or... if you wanna be real C++ like... try using the STL bitset class... I've never done so myself, but here's the docs:
http://www.sgi.com/tech/stl/bitset.html
and in fact, if you don't need a full 8bits (if you were using a character), this should be able to do less (since it's comparable to vector<bool>).
Elijah
11-25-2002, 03:49 AM
I know the basics of c++ but what does '->' and '%d' mean? I've only taken one course in c++ y'know :)
from the info I've gathered so far, it seems that I'll need to use an array here ... but the tricky part could come in dividing the poly and the message....
->, he is just using as an arrow to show the answer
the %d is replaced by the "z" variable by the printf function. It is a placeholder for an integer. The printf function is a C language function (that is why you haven't seen it). The "cout" function is usually used in C++ for outputting to the screen.
Elijah
11-25-2002, 05:30 PM
Originally posted by JayC
->, he is just using as an arrow to show the answer
the %d is replaced by the "z" variable by the printf function. It is a placeholder for an integer. The printf function is a C language function (that is why you haven't seen it). The "cout" function is usually used in C++ for outputting to the screen.
yeah, I kinda thought myself to use cout<< instead of printf, not actually used to C .... I'll be taking embedded C next term so I'll have to take it step-by-step. :) Thanks :)