Adobe Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Is it correct ?
int stride = M * 3;
int bit_offset = y * stride + x * 3;
or it should be below?
int bit_offset = x*strinde + y*3
// Assuming N rows and M columns
void change_pixels(char *grid, int N, int M, int x, int y, char new_pixel)
{
// stride per row in bits
int stride = M * 3;
int bit_offset = y * stride + x * 3;
int byte_offset = bit_offset / 8;
int shift = bit_offset % 8;
// read two bytes in case pixels are on byte boundary
short int *tmp = (short *)(grid + byte_offset);
short int tmp_pixels = *tmp;
// store surrounding pixel bits (shift, one's complement and AND), and insert (OR) our new pixel bits
tmp_pixels = tmp_pixels & ~(7 << shift);
tmp_pixels = tmp_pixels | ((short int)new_pixel << shift);
*tmp = tmp_pixels;
}
#include <stdio.h>
int main(void) {
char mat[3][2] = {
'1','2', '3','4', '5', '6'
};
char *a = mat;
int x=1;
int y=1;
int col = 2;
char c = *((a + x*col) + y);
printf(" original value : %c\n", c);
c = c & 0x00;
char given = '3';
c = c | given;
printf(" modified value : %c\n", c);
return 0;
}
- Anonymous September 23, 2014