Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the most efficient way to subtract signed integral data in binary (bits)?
    primarykey
    data
    text
    <p>I'm working in C on a PC, trying to leverage as little C++ as possible, working with binary data stored in unsigned char format, although other formats are certainly possible if worthwhile. The goal is subtracting two signed integer values (which can be ints, signed ints, longs, signed longs, signed shorts, etc.) in binary without converting to other data formats. The raw data is just prepackaged as unsigned char, though, with the user basically knowing which of the signed integer formats should be used for reading (i.e. we know how many bytes to read at once). Even though data is stored as an unsigned char array, data are meant to be read signed as two's-complement integers.</p> <p>One common way we're often taught in school is adding the negative. Negation, in turn, is often taught to be performed as flipping bits and adding 1 (0x1), resulting in two additions (perhaps a bad thing?); or, as other posts point out, flipping bits past the first zero starting from the MSB. I'm wondering if there is a more efficient way, that may not be easily described as a pen-and-paper operation, but works because of the way data is stored in bit format. Here are some prototypes I've written, which may not be the most efficient way, but which summarizes my progress so far based on textbook methodology.</p> <p>The addends are passed by reference in case I have to manually extend them to balance their length. Any and all feedback will be appreciated! Thanks in advance for considering.</p> <pre><code>void SubtractByte(unsigned char* &amp; a, unsigned int &amp; aBytes, unsigned char* &amp; b, unsigned int &amp; bBytes, unsigned char* &amp; diff, unsigned int &amp; nBytes) { NegateByte(b, bBytes); // a - b == a + (-b) AddByte(a, aBytes, b, bBytes, diff, nBytes); // Restore b to its original state so input remains intact NegateByte(b, bBytes); } void AddByte(unsigned char* &amp; a, unsigned int &amp; aBytes, unsigned char* &amp; b, unsigned int &amp; bBytes, unsigned char* &amp; sum, unsigned int &amp; nBytes) { // Ensure that both of our addends have the same length in memory: BalanceNumBytes(a, aBytes, b, bBytes, nBytes); bool aSign = !((a[aBytes-1] &gt;&gt; 7) &amp; 0x1); bool bSign = !((b[bBytes-1] &gt;&gt; 7) &amp; 0x1); // Add bit-by-bit to keep track of carry bit: unsigned int nBits = nBytes * BITS_PER_BYTE; unsigned char carry = 0x0; unsigned char result = 0x0; unsigned char a1, b1; // init sum for (unsigned int j = 0; j &lt; nBytes; ++j) { for (unsigned int i = 0; i &lt; BITS_PER_BYTE; ++i) { a1 = ((a[j] &gt;&gt; i) &amp; 0x1); b1 = ((b[j] &gt;&gt; i) &amp; 0x1); AddBit(&amp;a1, &amp;b1, &amp;carry, &amp;result); SetBit(sum, j, i, result==0x1); } } // MSB and carry determine if we need to extend: if (((aSign &amp;&amp; bSign) &amp;&amp; (carry != 0x0 || result != 0x0)) || ((!aSign &amp;&amp; !bSign) &amp;&amp; (result == 0x0))) { ++nBytes; sum = (unsigned char*)realloc(sum, nBytes); sum[nBytes-1] = (carry == 0x0 ? 0x0 : 0xFF); //init } } void FlipByte (unsigned char* n, unsigned int nBytes) { for (unsigned int i = 0; i &lt; nBytes; ++i) { n[i] = ~n[i]; } } void NegateByte (unsigned char* n, unsigned int nBytes) { // Flip each bit: FlipByte(n, nBytes); unsigned char* one = (unsigned char*)malloc(nBytes); unsigned char* orig = (unsigned char*)malloc(nBytes); one[0] = 0x1; orig[0] = n[0]; for (unsigned int i = 1; i &lt; nBytes; ++i) { one[i] = 0x0; orig[i] = n[i]; } // Add binary representation of 1 AddByte(orig, nBytes, one, nBytes, n, nBytes); free(one); free(orig); } void AddBit(unsigned char* a, unsigned char* b, unsigned char* c, unsigned char* result) { *result = ((*a + *b + *c) &amp; 0x1); *c = (((*a + *b + *c) &gt;&gt; 1) &amp; 0x1); } void SetBit(unsigned char* bytes, unsigned int byte, unsigned int bit, bool val) { // shift desired bit into LSB position, and AND with 00000001 if (val) { // OR with 00001000 bytes[byte] |= (0x01 &lt;&lt; bit); } else{ // (!val), meaning we want to set to 0 // AND with 11110111 bytes[byte] &amp;= ~(0x01 &lt;&lt; bit); } } void BalanceNumBytes (unsigned char* &amp; a, unsigned int &amp; aBytes, unsigned char* &amp; b, unsigned int &amp; bBytes, unsigned int &amp; nBytes) { if (aBytes &gt; bBytes) { nBytes = aBytes; b = (unsigned char*)realloc(b, nBytes); bBytes = nBytes; b[nBytes-1] = ((b[0] &gt;&gt; 7) &amp; 0x1) ? 0xFF : 0x00; } else if (bBytes &gt; aBytes) { nBytes = bBytes; a = (unsigned char*)realloc(a, nBytes); aBytes = nBytes; a[nBytes-1] = ((a[0] &gt;&gt; 7) &amp; 0x1) ? 0xFF : 0x00; } else { nBytes = aBytes; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload