Help with bitwise operation (i think :P)

Programming, for all ages and all languages.
Post Reply
claesson92
Posts: 10
Joined: Wed Jun 06, 2007 12:26 am

Help with bitwise operation (i think :P)

Post by claesson92 »

I have a function looking like this:

Code: Select all

void scroll(size_t lines) {
    if(!lines) return; /* nothing to scroll */
 
    /* copy memory from "X position = top+lines" to top */
    memcpy(video_mem,
           video_mem+((SCREENCOLS*lines)<<1),
           ((SCREENROWS-lines)*SCREENCOLS)<<1);
 
    /* Then zero the other lines below the copied lines, which
       is the same amount as the number lines that we've scrolled. */
    memsetw(video_mem+(((SCREENROWS-lines)
                        *SCREENCOLS)<<1), /* multiply by 2 */
            ((k_text_color_get() & 0x70)<<8) /* fill with bgcolor */,
            ((SCREENCOLS*lines)) /* amount of lines to copy */);
 
    /* If we're not yet at the top of the screen,
     * move the "Y position" up by X lines. */
    if(screen_ypos >= lines) screen_ypos -= lines;
}

Code: Select all

k_text_color_get()
returns a byte (i.e 0x07).

I need some help understanding what this does:

Code: Select all

((k_text_color_get() & 0x70)<<8)
I Have a variable containing both text and background color. How can i then modify this functio to use that variable?

Thanks in advance
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

Post by Zacariaz »

"((k_text_color_get() & 0x70)<<8)"
well, lets split it of.
0x70 is the equivalent of 01110000b

i have no idea what value k_text_color_get() returns, but lets asume that its 0101 0101b

then

0101 0101b &
0111 0000b ==
0101 0000b

then we take 01010000b and shift it left by 8 bits which basicly just means that we'll add 8 zeroz so we get the result: 0101 0000 0000 0000b which equals 0x5000

thats binary operators for you. as for the function, i think im too drunk to fully comprihend it.
This was supposed to be a cool signature...
User avatar
hailstorm
Member
Member
Posts: 110
Joined: Wed Nov 02, 2005 12:00 am
Location: The Netherlands

Post by hailstorm »

Zacariaz explained the technical working of this part of code.
As far as I can see, it takes the text attribute (fore and background color), takes out the background color and it disables / sets of (or better put: takes out) the blinking bit.

This is also commented in the code fragment.

Am I right, fellows? :?:
claesson92
Posts: 10
Joined: Wed Jun 06, 2007 12:26 am

Post by claesson92 »

Maybe i got it all wrong. But does this use the text color returned by k_text_get_color() on the backgorund 0x70 (light gray) ?
User avatar
hailstorm
Member
Member
Posts: 110
Joined: Wed Nov 02, 2005 12:00 am
Location: The Netherlands

Post by hailstorm »

No, the routine is just calculating the background color append it to the null character and writes it to video memory.

In calculating the bg-color the following steps are performed:
- Get color/attribute (Fore and background color)
- Isolate background color (High nibble (and it with 0x70, so the intensity or blinking bit is disabled))
- Shift it one byte, so it will put up the high byte of a word...

It is used in a memset function to copy it into video memory. This means that the calculated backcolor is written to the odd bytes in video memory, so it determines the color of the written character, which, in this case, is left 0. I'd choose the space-character (0x20), but that's a whole different story.
Post Reply