-2

In C, I am given an int x and have to use only the following bitwise and logical operators to multiply x by 2: << >> ~ | ^ &. The operators + and * are specifically forbidden.

This would normally be easy, as I could just do x << 1. However, the goal of this problem is to assume that that x contains a single precision float value.

I believe the point of pretending that x is a float is to challenge us to to think about floats in bits and how to properly multiply them by applying the previously mentioned shift and logical operators.

When it comes to shifting floating point values, my current understanding is that a float's bits can be misrepresented when shifted. Is this correct? If not, why? Otherwise, my understanding is correct and I'm stuck on how to go about implementing this. Any help would be greatly appreciated!

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • When multiplying by 2, you're just increasing the exponent by 1. Of course, that addition isn't entirely trivial when `+` is not permitted. Do you have to worry about overflows, infinities and not-a-number values (NaNs). – Jonathan Leffler Feb 14 '19 at 22:51
  • 1
    You have duplicated your own question without fixing the issues with the original. Better to fix the question. Is it specifically multiply by 2 only you need - the title and the text seem to differ in this respect. – Clifford Feb 14 '19 at 23:43
  • 1
    I'm confused, was this not answered here [Simulating Floating Point Multiplication in C using Bitwise Operators](https://stackoverflow.com/questions/54610832/simulating-floating-point-multiplication-in-c-using-bitwise-operators) (4-days ago?) Somehow, we need to reopen and merge with the original question. – David C. Rankin Feb 14 '19 at 23:57
  • @DavidC.Rankin : It was marked as a duplicate, but the duplicate was already closed as "too broad" (not sure I agree), while reopening that requires a number of votes, I was able to reopen a "marked-as-duplicate" immediately so did so. Arrogantly believing that I had a better answer that the world _must_ see ;-). – Clifford Feb 15 '19 at 00:25
  • 1
    That makes sense. It was just strange when I clicked the linked question and it was essentially the same question just worded better, but there were a number of answers to the original which had 3-reopen votes pending (which I agree the placing on hold "too broad" was a bit questionable as it was clear what was asked) – David C. Rankin Feb 15 '19 at 00:28
  • @JonathanLeffler we do not have to worry about overflows, infinity cases, or NaN's. –  Feb 15 '19 at 00:43
  • @Clifford Sorry for the confusion (to both you and David), the earlier question you are referring too was marked as too broad, so I tried to simplify the problem in this post. In response to your question Clifford, we only need to multiply the number by 2. –  Feb 15 '19 at 00:46
  • @DavidC.Rankin I think I'll take your advice and merge this question with the previous one asked. This question will soon be deleted. –  Feb 15 '19 at 01:18
  • I'll vote to reopen the original there @Clifford should move his answer there as well if you are deleting this one. (done) – David C. Rankin Feb 15 '19 at 01:20
  • Possible duplicate of [Simulating Floating Point Multiplication in C using Bitwise Operators](https://stackoverflow.com/questions/54610832/simulating-floating-point-multiplication-in-c-using-bitwise-operators) – njuffa Feb 15 '19 at 01:36
  • @njuffa did you read the discussion above? I'd appreciate not being flagged unnecessarily. This question is now unable to be deleted because "others have invested time and effort into answering it." –  Feb 15 '19 at 02:33
  • This *is* a duplicate question. It should be closed because of that and I have voted accordingly. I have *also* voted to re-open the original question that was duplicated here. The original question seemed clear and specific enough to me and I provided an answer to it. – njuffa Feb 15 '19 at 02:56
  • @vastImmortalSuns the information about overflow, NaN etc. needs to be in the question, not a comment. It will prevent unnesssarily complex answers. Fixing your original question may prevent reclosure. – Clifford Feb 15 '19 at 07:58

1 Answers1

1

You need to increment the exponent by 1 to double the floating-point value. That can be done by a ripple-carry adder:

#include <stdio.h>
#include <stdint.h>

int main()
{
    float f = 3.14f ;           // Test value
    uint32_t* x = (int*)(&f) ;  // get "bits view" of test value

    // increment exponent
    uint32_t mask = 0x00800000 ;
    *x ^= mask ;
    while( (*x & mask) == 0 && 
           mask != 0x80000000 )
    {
        mask <<= 1 ;
        *x ^= mask ;
    } 

    // Show result
    printf( "%f", f ) ;

    return 0;
}

The output of the above code is :

6.280000

The solution does not deal with exponent overflow - that would require mantissa adjustment.

Clifford
  • 88,407
  • 13
  • 85
  • 165