Click here to Skip to main content
15,895,256 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: This bug makes me want to cry Pin
Daniel Pfeffer12-Apr-21 20:33
professionalDaniel Pfeffer12-Apr-21 20:33 
GeneralRe: This bug makes me want to cry Pin
honey the codewitch13-Apr-21 1:34
mvahoney the codewitch13-Apr-21 1:34 
GeneralRe: This bug makes me want to cry Pin
Fueled By Decaff12-Apr-21 5:22
Fueled By Decaff12-Apr-21 5:22 
GeneralRe: This bug makes me want to cry Pin
honey the codewitch12-Apr-21 5:25
mvahoney the codewitch12-Apr-21 5:25 
GeneralRe: This bug makes me want to cry Pin
Ron Nicholson12-Apr-21 5:25
professionalRon Nicholson12-Apr-21 5:25 
GeneralRe: This bug makes me want to cry Pin
Greg Utas12-Apr-21 5:28
professionalGreg Utas12-Apr-21 5:28 
GeneralRe: This bug makes me want to cry Pin
W Balboos, GHB12-Apr-21 5:38
W Balboos, GHB12-Apr-21 5:38 
GeneralRe: This bug makes me want to cry Pin
honey the codewitch12-Apr-21 5:50
mvahoney the codewitch12-Apr-21 5:50 
Here's the thing. The # of channels per pixel, and the bit depth of each channel is arbitrary.
I could turn around right now and define a 19 bit pixel and the thing still needs to work.

So I have shifty bits in my code to ... shift bits. Also to set bits on non-byte boundaries.

C++
static void shift_left(void* bits,size_t offset_bits,size_t size_bits, size_t shift) {
    if(nullptr==bits || 0==size_bits || 0==shift) {
        return;
    }
    // special case if we shift all the bits out
    if(shift>=size_bits) {
        set_bits(bits,offset_bits,size_bits,false);
        return;
    }
    uint8_t* pbegin = ((uint8_t*)bits)+(offset_bits/8);
    const size_t offset = offset_bits % 8;
    const size_t shift_bytes = shift / 8;
    const size_t shift_bits = shift % 8;
    const size_t overhang = (size_bits+offset_bits) % 8;
    // preserves left prior to offset
    const uint8_t left_mask = ((uint8_t)uint8_t(0xFF<<(8-offset)));
    // preserves right after overhang
    const uint8_t right_mask = 0!=overhang?uint8_t(0xFF>>overhang):0;
    uint8_t* pend = pbegin+(size_t)((offset_bits+size_bits)/8.0+.999999);
    uint8_t* plast = pend-1;
    uint8_t* psrc = pbegin+shift_bytes;
    uint8_t* pdst = pbegin;
    if(pbegin+1==pend) {
        // special case for a shift all within one byte
        uint8_t save_mask = left_mask|right_mask;
        uint8_t tmp = *pbegin;
        *pbegin = uint8_t(uint8_t(tmp<<shift_bits)&~save_mask)|
                uint8_t(tmp&save_mask);
        return;
    }
    // preserve the ends so we can
    // fix them up later
    uint8_t left = *pbegin;
    uint8_t right = *(pend-1);
    
    while(pdst!=pend) {
        uint8_t src = psrc<pend?*psrc:0;
        uint8_t src2 = (psrc+1)<pend?*(psrc+1):0;
        *pdst = (src<<shift_bits)|(src2>>(8-shift_bits));
        ++psrc;
        ++pdst;
    }
    
    *pbegin=(left&left_mask)|uint8_t(*pbegin&~left_mask);
    --pend;
    *plast=uint8_t(right&right_mask)|uint8_t(*plast&uint8_t(~right_mask));
};


I'll exclude shift_right, plus all the templatized versions of these functions that the compiler computes at compile time instead of run time

and

C++
inline static void set_bits(size_t offset_bits,size_t size_bits,void* dst,const void* src) {
    const size_t offset_bytes = offset_bits / 8;
    const size_t offset = offset_bits % 8;
    const size_t total_size_bytes = (offset_bits+size_bits)/8.0+.999999999;
    const size_t overhang = (offset_bits+size_bits) % 8;
    uint8_t* pbegin = ((uint8_t*)dst)+offset_bytes;
    uint8_t* psbegin = ((uint8_t*)src)+offset_bytes;
    uint8_t* pend = ((uint8_t*)dst)+total_size_bytes;
    uint8_t* plast = pend-(pbegin!=pend);

    const uint8_t maskL = 0!=offset?
                                (uint8_t)((uint8_t(0xFF>>offset))):
                                uint8_t(0xff);
    const uint8_t maskR = 0!=overhang?
                            (uint8_t)~((uint8_t(0xFF>>overhang))):
                            uint8_t(0xFF);
    if(pbegin==plast) {
        uint8_t v = *psbegin;
        const uint8_t mask = maskL & maskR;
        v&=mask;
        *pbegin&=~mask;
        *pbegin|=v;
        return;
    }
    *pbegin&=~maskL;
    *pbegin|=((*psbegin)&maskL);
    *plast&=~maskR;
    *plast|=((*(psbegin+total_size_bytes-1))&maskR);
    if(pbegin+1<plast) {
        const size_t len = plast-(pbegin+1);
        if(0!=len&&len<=total_size_bytes) 
            memcpy(pbegin+1,psbegin+1,len);
    }
}

Real programmers use butterflies

GeneralRe: This bug makes me want to cry Pin
Sander Rossel12-Apr-21 10:35
professionalSander Rossel12-Apr-21 10:35 
GeneralMessage Closed Pin
12-Apr-21 2:41
Maria Roye12-Apr-21 2:41 
GeneralRe: want to Learn Development Pin
Greg Utas12-Apr-21 3:04
professionalGreg Utas12-Apr-21 3:04 
GeneralRe: want to Learn Development Pin
Richard Deeming12-Apr-21 3:10
mveRichard Deeming12-Apr-21 3:10 
GeneralRe: want to Learn Development Pin
Greg Utas12-Apr-21 3:13
professionalGreg Utas12-Apr-21 3:13 
GeneralMessage Closed Pin
12-Apr-21 3:36
Maria Roye12-Apr-21 3:36 
GeneralRe: want to Learn Development Pin
Greg Utas12-Apr-21 3:44
professionalGreg Utas12-Apr-21 3:44 
GeneralRe: want to Learn Development Pin
Richard Deeming12-Apr-21 4:37
mveRichard Deeming12-Apr-21 4:37 
GeneralThis one is also hidden link spam Pin
OriginalGriff12-Apr-21 4:43
mveOriginalGriff12-Apr-21 4:43 
GeneralRe: want to Learn Development Pin
PIEBALDconsult12-Apr-21 3:04
mvePIEBALDconsult12-Apr-21 3:04 
GeneralRe: want to Learn Development Pin
Richard Deeming12-Apr-21 3:11
mveRichard Deeming12-Apr-21 3:11 
GeneralRe: want to Learn Development Pin
PIEBALDconsult12-Apr-21 3:14
mvePIEBALDconsult12-Apr-21 3:14 
GeneralRe: want to Learn Development Pin
Richard MacCutchan12-Apr-21 3:23
mveRichard MacCutchan12-Apr-21 3:23 
General[SPAM] want to Learn Development Pin
Richard Deeming12-Apr-21 3:11
mveRichard Deeming12-Apr-21 3:11 
GeneralRe: [SPAM] want to Learn Development Pin
OriginalGriff12-Apr-21 3:15
mveOriginalGriff12-Apr-21 3:15 
AnswerRe: want to Learn Development Pin
W Balboos, GHB12-Apr-21 3:14
W Balboos, GHB12-Apr-21 3:14 
GeneralRe: want to Learn Development Pin
OriginalGriff12-Apr-21 3:16
mveOriginalGriff12-Apr-21 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.