0

I'm trying to create a program to add two IEEE 754 floating point numbers, I'm pretty far into the program but I've come to a halt in the bit shift section of the algorithm.

I have created a tmp variable to copy the mantissa(0's and 1's inside the char array to be shifted to the right). Then I copy the tmp variable into the previous variable in the spots needed... Any help or suggestions will be appreciated... here is my code that needs reworking.

if( e1 > e2 ){ // If exponent 1 is greater than exponent 2 shift exponent 2
    k = e1 - e2;  // k = size of shift
    for(i = 11; i < 40; i++)  // for loop for original array
      for(j = 1; j < 30; j++)  // for loop for tmp array
        tmp_bit_string[j] = bit_string2[i];  // copy original into tmp

    *tmp_bit_string >>= k; //shift tmp

    for(i = 11; i < 40; i++) //second loop to copy tmp back into original
      for(j = 1; j < 30; j++)
        tmp_bit_string[j] = bit_string2[i];
  }


  if( e2 > e1 ){  //same thing but opposite of previous loop
    k = e2 - e1;
    for(i = 11; i < 40; i++)
      for(j = 1; j < 30; j++)
        tmp_bit_string[j] = bit_string[i];

    *tmp_bit_string >>= k;

    for(i = 11; i < 40; i++)
      for(j = 1; j < 30; j++)
        tmp_bit_string[j] = bit_string[i];
  }
2
Contributors
3
Replies
6
Views
6 Years
Discussion Span
Last Post by N1GHTS
0

Oh jeez, this is entirely the realm of assembly language. Why don't you use asm{} to get this done in like 15 lines of code or less? You can take advantage of the CPU zero and carry flags in byte shifts there.

0

yeah, its for the class computer architecture... fun stuff.

but the teacher is allowing us to only use the shift and integer add operations in c to do the addition, so i dont think using asm would be allowed...

0

Not sure why it wouldn't be allowed in a class named for computer architecture, a realm completely dominated by MOSFETs and flip-flops, yet your limited to C of all things.

Anyways, while I am not entirely sure what you are asking, here is how I would bit shift across 2 chars:

unsigned char H, // High byte
              L, // Low byte
              T; // Temp byte

// To shift both of them to the left...

T = L & 0x80; // Grab the upper bit 
              // (to compensate for C not having a carry flag)
L <<= 1;      // Shift the low byte left by 1 bit. Lowest bit is now zero.
H <<= 1;      // Shift the high byte left by 1 bit.
H |= (T>0);   // If T is not zero, the expression 
              // will evaluate to 1. So this merges
              // The high bit shifted by L into the low
              // bit in H which should is currently zero.

// At this point, both H and L have been shifted left
// as one unit. You can do this across as many bytes as
// you need. You can optionally save the highest bit
// before it gets eaten up by the shifts by performing 
// this test before the left shift operation...

int LostBit = ((H & 0x80)>0);

// Right shifting is about the same thing, only the
// opposite operations.

T = H & 0x01;   // Save the lowest bit of the highest byte
H >>= 1;        // High byte shift right
L >>= 1;        // Low byte shift right
L |= (T==0)?(0):(0x80);  // If the saved byte is not zero,
                         // merge H's lowest bit to L's  
                         // highest bit.

Hope this helps you find a solution to your issue.

Edited by N1GHTS: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.