Modificar valor

Iniciado por Choke1, 6 Marzo 2015, 17:28 PM

0 Miembros y 2 Visitantes están viendo este tema.

Choke1

gracias por la ayuda y la paciencia.

Mira esto quiero que me saga:

Todos los bits del pixel puestos a 0
+----------------+----------------+----------------+----------------+
| ALFA | ROJO | VERDE | AZUL |
+----------------+----------------+----------------+----------------+
| 0 0 0 0 0 0 0 0| 0 0 0 0 0 0 0 0| 0 0 0 0 0 0 0 0| 0 0 0 0 0 0 0 0|

Poner a 1 el bit menos significativo del canal Alfa (bit 24)
+----------------+----------------+----------------+----------------+
| ALFA | ROJO | VERDE | AZUL |
+----------------+----------------+----------------+----------------+
| 0 0 0 0 0 0 0 1| 0 0 0 0 0 0 0 0| 0 0 0 0 0 0 0 0| 0 0 0 0 0 0 0 0|

poner solo el bit8 a con valor 1

ivancea96

Pues b.bit7.

En cualquier caso, no te recomendaría usar esa union. Si quieres poner a 1 un bit, basta hacer:

num = num | 0b00000001;

En ese ejemplo, si num es un char, 8 bits, le pondría el primer bit a 1.

Aplica operaciones binarias a nivel de bit: AND &, OR |, XOR ^.

Choke1

Nada sigo sin conseguirlo XD, me obligan a usar esa union.

ivancea96

Pon el fragmento de código, entre las etiquetas GeSHi, para que lo vea.

Choke1



typedef struct TreintaydosBits {
    unsigned bit0 : 1;
    unsigned bit1 : 1;
    unsigned bit2 : 1;
    unsigned bit3 : 1;
    unsigned bit4 : 1;
    unsigned bit5 : 1;
    unsigned bit6 : 1;
    unsigned bit7 : 1;
    unsigned bit8 : 1;
    unsigned bit9 : 1;
    unsigned bit10 : 1;
    unsigned bit11 : 1;
    unsigned bit12 : 1;
    unsigned bit13 : 1;
    unsigned bit14 : 1;
    unsigned bit15 : 1;
    unsigned bit16 : 1;
    unsigned bit17 : 1;
    unsigned bit18 : 1;
    unsigned bit19 : 1;
    unsigned bit20 : 1;
    unsigned bit21 : 1;
    unsigned bit22 : 1;
    unsigned bit23 : 1;
    unsigned bit24 : 1;
    unsigned bit25 : 1;
    unsigned bit26 : 1;
    unsigned bit27 : 1;
    unsigned bit28 : 1;
    unsigned bit29 : 1;
    unsigned bit30 : 1;
    unsigned bit31 : 1;
} TreintaydosBits;

typedef union CuatroBytes {
    long n;
    TreintaydosBits b;
} CuatroBytes;

/**----- Prototipos -----**/
void ImprimeBitsdePixel(long);

int main(int argc, char** argv) {

    CuatroBytes pixel;
    // Todos los bits del pixel los ponemos a cero

    pixel.n = 0;

    printf("\nTodos los bits del pixel puestos a 0\n");
    ImprimeBitsdePixel(pixel.n);
    printf("\n");
    pixel.b.bit10 = 1;
    printf("\nPoner a 1 el bit menos significativo del canal Alfa (bit 24)\n");
    ImprimeBitsdePixel(pixel.n);

    return (EXIT_SUCCESS);
}

void ImprimeBitsdePixel(long nump) {

    int i;

    printf("\n+----------------+----------------+----------------+----------------+\n"
            "|      ALFA      |      ROJO      |      VERDE     |      AZUL      |\n"
            "+----------------+----------------+----------------+----------------+\n");
    for (i = 0; i < 32; i++) {
        printf("%ld ", nump);

        if (i == 7 || i == 15 || i == 23) {
            printf("  ");
        }
    }

}

ivancea96

En el printf de la función estás imprimiendo el número entero.

if(nump&(1<<i))
    printf("1 ");
else
    printf("0 ");


Así imprimes solo cada bit.

Choke1

Muchas Gracias!! Te has ganado el cielo jeje  ;-)


Choke1

Y si xj quiero cambiar 8 tengo que cambiarlo de uno  a uno, xj asi:


pixel.b.bit8 = 1;
pixel.b.bit9 = 1;
pixel.b.bit10 = 1;
pixel.b.bit11 = 1;
...

O existe alguna manera menos chapuzera?

ivancea96

La forma correcta sería la que puse antes, con operadores binarios. Pero decías que no te dejaban :o

Choke1

Por cierto que significa la condicion esa: if(nump&(1<<i))