izmenjeno 1 put, poslednji put Lazoman 17.10.2014. u 13:11
Izmene poruke
- Lazoman17.10.2014. u 13:11zapis iz teksta poruke
Problem koji imam je u vezi operacija sa bitovima, kao i zadacima na kraju poglavlja koji se nalaze u knjizi O Reilly - Practical C Programming, 3rd Edition koju trenutno čitam.
Dakle da li može neko da mi pojasni #define SET_BIT(x,y) graphics[(x)/8][y] |= (0x80 >>((x)%8)) , sta se ustvari desava i kada, koji se bit setuje i kako da protumacim graphics [(x)/8][y] |= (0x80 >>((x)%8)), znam da je makro u pitanju i jasno mi je kako on funkcionise ali mešanje aritmetičkih operatora i operatora za rad nad bitovima me buni i to dosta.
Kod programa je sledeci:
Hvala unapred na odgovoru :)
Dakle da li može neko da mi pojasni #define SET_BIT(x,y) graphics[(x)/8][y] |= (0x80 >>((x)%8)) , sta se ustvari desava i kada, koji se bit setuje i kako da protumacim graphics [(x)/8][y] |= (0x80 >>((x)%8)), znam da je makro u pitanju i jasno mi je kako on funkcionise ali mešanje aritmetičkih operatora i operatora za rad nad bitovima me buni i to dosta.
Kod programa je sledeci:
Code:
#include <stdio.h>
#define X_SIZE 40 /* size of array in X direction */
#define Y_SIZE 60 /* size of array in Y direction */
/*
* We use X_SIZE/8 because we pack 8 bits per byte
*/
char graphics[X_SIZE / 8][Y_SIZE]; /* the graphics data */
#define SET_BIT(x,y) graphics[(x)/8][y] |= (0x80 >>((x)%8))
int main()
{
int loc; /* current location we are setting */
void print_graphics(void); /* print the data */
for (loc = 0; loc < X_SIZE; ++loc)
SET_BIT(loc, loc);
print_graphics();
return (0);
}
/********************************************************
* print_graphics -- Prints the graphics bit array *
* as a set of X and .'s. *
********************************************************/
void print_graphics(void)
{
int x; /* current x BYTE */
int y; /* current y location */
unsigned int bit; /* bit we are testing in the current byte */
for (y = 0; y < Y_SIZE; ++y) {
/* Loop for each byte in the array */
for (x = 0; x < X_SIZE / 8; ++x) {
/* Handle each bit */
for (bit = 0x80; bit > 0; bit = (bit >> 1)) {
if ((graphics[x][y] & bit) != 0)
printf("X");
else
printf(".");
}
}
printf("\n");
}
}
#include <stdio.h>
#define X_SIZE 40 /* size of array in X direction */
#define Y_SIZE 60 /* size of array in Y direction */
/*
* We use X_SIZE/8 because we pack 8 bits per byte
*/
char graphics[X_SIZE / 8][Y_SIZE]; /* the graphics data */
#define SET_BIT(x,y) graphics[(x)/8][y] |= (0x80 >>((x)%8))
int main()
{
int loc; /* current location we are setting */
void print_graphics(void); /* print the data */
for (loc = 0; loc < X_SIZE; ++loc)
SET_BIT(loc, loc);
print_graphics();
return (0);
}
/********************************************************
* print_graphics -- Prints the graphics bit array *
* as a set of X and .'s. *
********************************************************/
void print_graphics(void)
{
int x; /* current x BYTE */
int y; /* current y location */
unsigned int bit; /* bit we are testing in the current byte */
for (y = 0; y < Y_SIZE; ++y) {
/* Loop for each byte in the array */
for (x = 0; x < X_SIZE / 8; ++x) {
/* Handle each bit */
for (bit = 0x80; bit > 0; bit = (bit >> 1)) {
if ((graphics[x][y] & bit) != 0)
printf("X");
else
printf(".");
}
}
printf("\n");
}
}
Hvala unapred na odgovoru :)
