Bit a bit O no funciona C [duplicado]

Please help me with this code. I don't know why the output is not 8.

#include <stdio.h>
#include <stdlib.h>
int main(){
    char c;
    c = c & 0;
    printf("The value of c is %d", (int)c);
    int j = 255;
    c = (c | j);
    int i =0;
    while( (c & 1) == 1){
        c = c>>1;
    printf("the value of i is %d",i);
    return 0;

To all the people who gave me -1, I tried printing the values. But it goes into an infinite loop.

preguntado el 27 de agosto de 11 a las 14:08

Have you tried printing out intermediate values of your variables? -

Didn't you just ask a very similar question about an hour ago? Also, how about an attempt at debugging this yourself? It's not that hard, add a printf in the while loop and print the value of c. You'd see immediately why you're in an infinite loop. I'd imagine you wouldn't be getting down voted if you'd asked about why the right shift wasn't working as you wanted it to. -

3 Respuestas

As you learnt from tu pregunta anterior, char is signed on your platform. Therefore, at the beginning of your while lazo, c has a negative value.

Right-shifting a negative value is implementation-defined. On your platform, I imagine it is doing an cambio aritmético.

contestado el 23 de mayo de 17 a las 14:05

I didn't realize right shifting a negative value was implementation defined. If you perform a logical right shift on a negative value you end up with a positive value, which doesn't make any sense. It'd seem like C should've mandated signed integer right shifting to be arithmetic shifting. - pretoriano

@Praetorian: It's implementation-defined because different hardware units will do different things (some will do a logical shift, some will do an arithmetic shift). Mandating a particular behaviour would "penalise" any implementation that didn't natively support the mandated behaviour. - Oliver Charlesworth

True, but mandating it might also have persuaded the guys designing the silicon for those processors to implement arithmetic shift :-) But we gotta live with what we got. - pretoriano

Honestly, I think this is one place where C is being stupid. If you don't care about arithmetic-shift behavior on negative values, you should be using unsigned quantities. If you do care, you want the "right" answer, not useless implementation-defined behavior. Bitshift on signed values never makes sense unless you want it for arithmetic-shift purposes. - R .. GitHub DEJA DE AYUDAR A ICE

This code has undefined behavior on the very first statement. c is uninitialized and has an indeterminate value, and using an object with indeterminate value, even to bitwise-and or multiply it by zero, results in undefined behavior.

Respondido 27 ago 11, 19:08

Esto no parecer to answer the question, yet raises a really interesting issue: you can't recover from UD. - xtofl

Intente usar un unsigned char. The standard C char can be signed (and quite often is on PC).

Respondido 27 ago 11, 19:08

Actually the standard does not define whether char está firmado o sin firmar. - Mankarse

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.