Click here to Skip to main content
15,888,802 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: C++ is such a lovely, infuriating puzzlebox Pin
Gary R. Wheeler6-Mar-22 12:50
Gary R. Wheeler6-Mar-22 12:50 
GeneralRe: C++ is such a lovely, infuriating puzzlebox Pin
Mike Hankey6-Mar-22 13:01
mveMike Hankey6-Mar-22 13:01 
GeneralRe: C++ is such a lovely, infuriating puzzlebox Pin
Gary R. Wheeler6-Mar-22 12:56
Gary R. Wheeler6-Mar-22 12:56 
GeneralRe: C++ is such a lovely, infuriating puzzlebox Pin
honey the codewitch6-Mar-22 13:32
mvahoney the codewitch6-Mar-22 13:32 
GeneralRe: C++ is such a lovely, infuriating puzzlebox Pin
Jacob Williamss6-Mar-22 16:49
Jacob Williamss6-Mar-22 16:49 
GeneralRe: C++ is such a lovely, infuriating puzzlebox Pin
honey the codewitch6-Mar-22 17:07
mvahoney the codewitch6-Mar-22 17:07 
GeneralRe: C++ is such a lovely, infuriating puzzlebox Pin
BernardIE53176-Mar-22 16:57
BernardIE53176-Mar-22 16:57 
GeneralRe: C++ is such a lovely, infuriating puzzlebox Pin
honey the codewitch6-Mar-22 17:03
mvahoney the codewitch6-Mar-22 17:03 
By narrow, I just meant code that performs a particular task or something.

Like making this return the nearest integer type that can contain data of the specified bit depth:

C++
bits::uintx<bits::get_word_size(BitDepth)>


Or a more complicated example, shifting an arbitrary number of bits an arbitrary number of digits left at compile time:

C++
nstexpr 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+7)/8);
    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));
};

To err is human. Fortune favors the monsters.

GeneralYay Pin
Mike Hankey5-Mar-22 11:31
mveMike Hankey5-Mar-22 11:31 
GeneralRe: Yay Pin
Mircea Neacsu5-Mar-22 12:27
Mircea Neacsu5-Mar-22 12:27 
GeneralRe: Yay Pin
Mike Hankey5-Mar-22 12:40
mveMike Hankey5-Mar-22 12:40 
GeneralRe: Yay Pin
Sander Rossel5-Mar-22 23:41
professionalSander Rossel5-Mar-22 23:41 
GeneralRe: Yay Pin
Richard MacCutchan6-Mar-22 1:23
mveRichard MacCutchan6-Mar-22 1:23 
GeneralRe: Yay Pin
Richard Andrew x646-Mar-22 4:26
professionalRichard Andrew x646-Mar-22 4:26 
GeneralRe: Yay Pin
Mike Hankey6-Mar-22 7:25
mveMike Hankey6-Mar-22 7:25 
JokeRe: Yay Pin
den2k886-Mar-22 21:30
professionalden2k886-Mar-22 21:30 
GeneralI'm being outsmarted by my workspace Pin
honey the codewitch5-Mar-22 6:36
mvahoney the codewitch5-Mar-22 6:36 
GeneralRe: I'm being outsmarted by my workspace Pin
Mike Hankey5-Mar-22 6:56
mveMike Hankey5-Mar-22 6:56 
GeneralRe: I'm being outsmarted by my workspace Pin
Franc Morales5-Mar-22 9:19
Franc Morales5-Mar-22 9:19 
GeneralRe: I'm being outsmarted by my workspace Pin
Mike Hankey5-Mar-22 9:28
mveMike Hankey5-Mar-22 9:28 
GeneralRe: I'm being outsmarted by my workspace Pin
Greg Utas5-Mar-22 7:02
professionalGreg Utas5-Mar-22 7:02 
GeneralRe: I'm being outsmarted by my workspace Pin
PIEBALDconsult5-Mar-22 8:30
mvePIEBALDconsult5-Mar-22 8:30 
GeneralRe: I'm being outsmarted by my workspace Pin
RickZeeland5-Mar-22 8:05
mveRickZeeland5-Mar-22 8:05 
GeneralRe: I'm being outsmarted by my workspace Pin
Joan M5-Mar-22 8:44
professionalJoan M5-Mar-22 8:44 
GeneralRe: I'm being outsmarted by my workspace Pin
honey the codewitch5-Mar-22 9:21
mvahoney the codewitch5-Mar-22 9:21 

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.