1. ## Working outside bytes......

Hi, i am trying to divide two long binary numbers for example.....

Code:
`unsigned char b =10101010101111010101100`
Code:
`unsigned char a =101000000110101111101100`
i want to divide b by a using the & operator...... (b/a).....can only work with bytes of data when approaching through strings.....as it is a bytewise operation.

is there another way or trick?

help would be most appreciated.

thanks

2. >i am trying to divide two long binary numbers for example.....
Your examples aren't correct. First, it's entirely possible that the number of bits in an unsigned char is considerably less than the number that you're trying to use. Second, there's no such thing as a binary constant in C.

>i want to divide b by a using the & operator
Simulating division with the bitwise operators takes more work than just using bitwise AND. Can you be more specific about what you're trying to do?

3. Originally Posted by Prelude
>i am trying to divide two long binary numbers for example.....
Your examples aren't correct. First, it's entirely possible that the number of bits in an unsigned char is considerably less than the number that you're trying to use. Second, there's no such thing as a binary constant in C.

>i want to divide b by a using the & operator
Simulating division with the bitwise operators takes more work than just using bitwise AND. Can you be more specific about what you're trying to do?

> Your examples aren't correct. First, it's entirely possible that the number of bits in an unsigned char is considerably less than the number that you're trying to use. Second, there's no such thing as a binary constant in C.

The actual string is far far longer, a 50 character hex array which i have managed to fit into 25 bytes using bitshift operations. i have since "converted" it to binary, again, using bit shifting.

> Simulating division with the bitwise operators takes more work than just using bitwise AND. Can you be more specific about what you're trying to do?

i know i have to use a while loop with a shift of one bit in every loop. but im unsure after that. maybe an XOR? my aim is to calculate the 24 bit CRC for a hex input of 50 characters. i have the algorithms and the final thing is to do the division, and im stuck at that point.

if you could clarify what i need to do, id be most grateful

4. Originally Posted by durban
hi, thanks for the link, im having a look right now but cant get my head round it.

i think its better if i tried it the way i was doing it.....

although, i will use the page if i have to.

thanks alot

5. sorry!!, i'll explain again!!

i have two arrays. one is about 40 bytes long and the other 5 bytes long.

they are both full of the binary representation of a hex value.

what i want to do is to divide the longer array by the smaller one.
this means i have to look for the most significant bit of both arrays and shift the smaller one by this number of bits.

this may be an exact multiple of 8 or may have a remainder ( X bytes and y bits). in which case, i have to shift the WHOLE string along Y bits. to allow the XOR of the two strings.

therefore, i was asking if there is a function which will

a) look for the most significant bit of each array and return the number of bits which have to be shifted?

i know that the shifting is harder as i have to carry the most significant bit of each array to the next one and so on.

also, as an aside, is the MSB of every string at address 0, if some data is captured and written into a string starting from bit 0?

thanks

6. You're still being extremely vague.

You said "they are both full of the binary representation of a hex value." This could mean a number of things, and if it means what I think it means, then it doesn't make sense. If it's a binary representation, then how can the value be intrinsically hex?

That's like saying "10101101" is the binary representation of a hex value, but it's not. It's the binary represenation of a value, which can then be represented in hex, decimal, octal, ternary, whatever. So, can you explain clearly what you want to do, perhaps with an example in C setting up the input arrays?

7. my original input was hex but i converted it to binary.

it could be an int or otherwise but im thinking in the mindset that IF i was to write it down in binary.

for example....

a1 is 01010001
and now from now on, im going to work with it in its binary representation....

8. Right, so the C equivalent is:
Code:
`a1 = 81;`
But again, what data types are you using in C for this? You've said array, but what kind?

Please give an example of the code in C. Give an example of your expected input, and your expected output, using real code in C. Then it will be clear as to what you want.

9. Originally Posted by cwr
Right, so the C equivalent is:
Code:
`a1 = 81;`
But again, what data types are you using in C for this? You've said array, but what kind?

Please give an example of the code in C. Give an example of your expected input, and your expected output, using real code in C. Then it will be clear as to what you want.

i initially have two strings....

the top one is the input and the lower one, a set hex divisor.

Code:
```     unsigned char numerator[40] = "acbabca2342353cbcaac4352c";
unsigned char denominator[10] = "cb1121212";```
this is then processed through a case statement and OR together. giving me a new string with the binary representation in the unsigned char with half the size of each. this is because i now have two "hex" characters in one byte.

the rest of the spaces which are empty are already
memset to 0
so i have a strings which MAY be written as

10000000000000000000000 and
00000000001000000000000

i want to search trough the top string to find the MSB and then use that output (X bytes and Y bits) for a different function to shift the lower string by X and Y bits so that the MSB's are exactly at the right power.

10. Right, so your original input is actually the representation of a hex value not in binary, but using your character set (99.9999999999999999% likely ASCII).

From your example above, the equivalent represenation of the numerator in "binary" would be:
Code:
`unsigned char numerator[] = { 0xAC, 0xBA, 0xCA, 0x23, 0x42, 0x35, 0x3C, 0xBC, 0xAA, 0xC4, 0x35, 0x2C };`
With regards to the rest of your questions, I still don't understand what you want to achieve? Are you trying to divide two very large hex numbers?

For example, 0xacbabca2342353cbcaac4352c divided by 0xcb1121212 is 0xA6F.8E3...

If not, then what?

As for functions, what exactly do you want to achieve? Break the problem into clear to understand pseudocode, with several functions, then attempt to write the functions, or ask for help?

11. Originally Posted by cwr
Right, so your original input is actually the representation of a hex value not in binary, but using your character set (99.9999999999999999% likely ASCII).

From your example above, the equivalent represenation of the numerator in "binary" would be:
Code:
`unsigned char numerator[] = { 0xAC, 0xBA, 0xCA, 0x23, 0x42, 0x35, 0x3C, 0xBC, 0xAA, 0xC4, 0x35, 0x2C };`
With regards to the rest of your questions, I still don't understand what you want to achieve? Are you trying to divide two very large hex numbers?

For example, 0xacbabca2342353cbcaac4352c divided by 0xcb1121212 is 0xA6F.8E3...

If not, then what?

As for functions, what exactly do you want to achieve? Break the problem into clear to understand pseudocode, with several functions, then attempt to write the functions, or ask for help?
everything you said in the first three paragraphs, i am trying to do. i am trying to divide two very large hex numbers.
but have taken the route of going down the"working with binary representation" road. this is because some bitwise manipulation must occur before the division occurs.

my question was that, IS there any function which will let me search through the two strings and look for the most significant bit in each array? that is all.

what i will later do is to get the offset value between the two strings and shift the bottom string by the amount of the offset.

right now, i just want to search the strings to find the most significant bit and get the exact bit addresses of each or the offset IN BITS.

thanks

12. Now I'm really confused. Division isn't just a case of bit shifting, but anyway. Is this for your own personal amusement, or an assignment, or what? If you're just trying to get the job done, there's the arbitrary precision math library called gmp, and the arbitrary precision calculator bc which will do exactly what you need.

If you're implementing this for your own curiousity, then are you aiming for speed or simplicity?

What do you mean by most significant bit in each array? Do you mean the most significant bit that is set to 1, or what? In an 8 bit byte, for example, the most significant bit is always bit 7. (counting 0 to 7 from right to left).

No, there is no predefined function in the standard that lets you do such a thing, but there are many simple ways of iterating through a byte or an array and inspecting the bits.

To clarify, let's say you have an array that is { 3, 2, 1}. This is represented in binary with 0000 0011 then 0000 0010 then 0000 0001. In that case, what do you consider "finding the most significant bit" to be? Do you mean the bit number of the first set (1) bit? In which case, just iterate through using the >> operator, or similar.

13. im doing it for an assignment but the rules are strict regarding the format of the variables. they HAVE to dealt with in their binary representation.
division will occur by me doing an XOR of the two strings and then shifting the divider by 1. then XOR again. until the longer string is too short to divide any further. i am actually trying to calculate a CRC of 24 bits.

the most significant bit, i means the highest bit which is set to one. meaning that that if i have a string with in one byte
0100 0000
the most significant bit is at position 1.

you were right in your final paragraph.

how would i iterate?

thanks

14. In canonical terminology, the bit you are talking about in 0100 0000 that is set to bit 1 is considered bit 6, because it goes from 7-0 in order of MSB to LSB.

Have a look at this FAQ on bitwise operations, particularly the bit about >> and <<, the shift operations, then try to think of a way of finding the MSB that is set to 1. Post an attempt at it, if you're still stuck, I'll try to help more. I suggest a for loop, repeating until you find a set bit, starting from the MSB. You may want to use the & operator too, see the FAQ.

Bear in mind that you are better off thinking hard about this things, so you understand it when more complicated tasks come up. Asking me to just give you code is not the best way to learn.

Also bear in mind that I about about to go to a bar, and get quite inebriated, and if you're unlucky, I will post responses after I return. The quality of them may be sub par.