从double中提取指数作为无符号值

时间:2019-06-15 14:16:57

标签: c

嗨,我对Codewars网站的一项练习有疑问。我的工作是从double值中提取指数作为无符号数。但是我不允许使用任何算术和二进制运算,也不能包含任何额外的文件。因此,我不能使用字符:+-* /%&| ^〜<>#?。我认为解决此问题的唯一方法是使用函数。您能给我一些建议吗,请执行此任务

func photoLibraryDidChange(_ changeInstance: PHChange) {

    ... 

    DispatchQueue.main.sync {
        let originalCount = fetchResult.count
        fetchResult = changes.fetchResultAfterChanges
        if changes.hasIncrementalChanges {

            guard let collectionView = self.collectionView else { return }

            collectionView.performBatchUpdates({

                if let removed = changes.removedIndexes, removed.count > 0 {
                    collectionView.deleteItems(at: removed.map({ IndexPath(item: originalCount - $0 - 1, section: 0) }))
                }
                if let inserted = changes.insertedIndexes, inserted.count > 0 {
                    collectionView.insertItems(at: inserted.map({ IndexPath(item: fetchResult.count - $0 - 1, section: 0) }))
                }
            })
            // Avoid deleting and reloading same index path at one update
            collectionView.performBatchUpdates({
                if let changed = changes.changedIndexes, changed.count > 0 {
                    collectionView.reloadItems(at: changed.map({ IndexPath(item: fetchResult.count - $0 - 1, section: 0) }))
                }
                changes.enumerateMoves { fromIndex, toIndex in
                    collectionView.moveItem(at: IndexPath(item: fetchResult.count - $0 - 1, section: 0),
                                            to: IndexPath(item: fetchResult.count - $0 - 1, section: 0))
                }
            })

        } else {
            collectionView!.reloadData()
        }

        ...

    }
}

1 个答案:

答案 0 :(得分:0)

注意:我尚未测试以下内容,因此可能需要进行一些“调整”

#include <math.h>
#include <stdint.h>

typedef union 
{
    double f;
    struct 
    {
        uint64_t mantisa : 52;
        uint64_t exponent : 11;
        uint64_t sign : 1;
    } parts;
} double_cast;

uint64_t normalizedExponent(double d)
{
    double_cast mydouble.f = d; 

    return ( mydouble.parts.sign )? mydouble.parts.exponent << 1:  mydouble.parts.exponent;

}