Tengo esta estructura y esta union:
typedef struct TreintaydosBits {
unsigned bit0 : 1;
unsigned bit1 : 1;
unsigned bit2 : 1;} TreintaydosBits;
typedef union CuatroBytes {
long n;
TreintaydosBits b;
} CuatroBytes;
y quiero cambiar a 0 el bit2.
He declarado lo siguiente:
CuatroBytes pixel;
Y pensaba que haciendo esto se me cambiaba
pixel.b.bit2 = 0;
Pero no ya que al imprimir me sale 256 256 256
¿Sabeis como se hace bn?
saluds
Esa estructura TreintaydosBits principalmente solo tiene 2 bits eh?
No entendí muy bien. Inicializaste n?
si si era por no pegar los 32.
Si hago lo siguiente:
// Todos los bits del pixel los ponemos a cero
pixel.n = 0;
No entiendo. Si n está inicializado a 0, hacer "pixel.b.bit2 = 0;" no hará nada.
La cosa es cambiar solo el valor de uno
pixel.n = 0; se ponen todos a 0
si solo solo quiero uno pensaba que se podia hacer asi pixel.b.bit2 = 0; pero no
pixel.b.bit2=1;
Eso hice y me pone 256 a todos los bits
#include <stdio.h>
typedef struct TreintaydosBits {
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;
} TreintaydosBits;
typedef union CuatroBytes {
short n;
TreintaydosBits b;
} CuatroBytes;
int main(){
CuatroBytes cb;
cb.n=0;
cb.b.bit3 = 1;
printf("%d",cb.n);
}
Eso es correcto.
Me imprime 8
El código que posteé ahí ha de imprimir 4. Imprime 8 si empiezas a numerar los bits desde 0.
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
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 ^.
Nada sigo sin conseguirlo XD, me obligan a usar esa union.
Pon el fragmento de código, entre las etiquetas GeSHi, para que lo vea.
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(" ");
}
}
}
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.
Muchas Gracias!! Te has ganado el cielo jeje ;-)
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?
La forma correcta sería la que puse antes, con operadores binarios. Pero decías que no te dejaban :o
Por cierto que significa la condicion esa: if(nump&(1<<i))
1 << 5 = 100000
Es un corrimiento de bits.
1<<i corre 1, i veces. Es para poder coger solo el bit i.
A ok Gracias !!