C/C++

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • Question about bitwise operators

    10 answers - 449 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    For some strange reason, I thought the following would give me a value
    of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.
    #include <stdio.h>
    int main(void) {
    unsigned long a = 678;
    unsigned long ff = 255;
    unsigned long c = a & ff;
    printf("The value is: %u \n", c);
    return 0;
    }
    The value is: 166
    $
    What am I missing here?
    Thanks in advance
    Chad
  • No.1 | | 225 bytes | |

    Chad wrote:
    For some strange reason, I thought the following would give me a value
    of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.
    Try it with 256 instead.
    678 is 0x2a6
    0xa6 is 166
  • No.2 | | 694 bytes | |

    Chad wrote:
    For some strange reason, I thought the following would give me a value
    of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.

    #include <stdio.h>

    int main(void) {
    unsigned long a = 678;
    unsigned long ff = 255;
    unsigned long c = a & ff;

    1010100110 (678)
    0011111111 (255)
    AND
    0010100110 (166)

    printf("The value is: %u \n", c);

    return 0;

    }
    The value is: 166
    $

    What am I missing here?

    That ANDing with 255 is equivalent to taking a number modulo 256, not 255.

    678 % 256 = 166, and repeated subtraction of 256 (678 - 256 - 256) will
    indeed give you 166.

    S.
  • No.3 | | 373 bytes | |

    Chad wrote:
    For some strange reason, I thought the following would give me a value
    of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.

    678 in binary = 1010100110
    255 in binary = 0011111111
    & together = 0010100110 = 166

    & = The bits in the result at set to 1 if the corresponding bits in the
    two operands are both 1.

  • No.4 | | 244 bytes | |


    pete wrote:
    Chad wrote:
    For some strange reason, I thought the following would give me a value
    of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.
    Try it with 256 instead.
    678 is 0x2a6
    0xa6 is 166
  • No.5 | | 255 bytes | |

    Chad wrote:
    pete wrote:
    Chad wrote:
    For some strange reason, I thought the following would give me a value
    of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.
    Try it with 256 instead.
    678 is 0x2a6
    0xa6 is 166
  • No.6 | | 255 bytes | |

    Chad wrote:
    pete wrote:
    Chad wrote:
    For some strange reason, I thought the following would give me a value
    of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.
    Try it with 256 instead.
    678 is 0x2a6
    0xa6 is 166
  • No.7 | | 1633 bytes | |

    Chad wrote:
    For some strange reason, I thought the following would give me a value
    of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.

    Why you think 'a & b' should mean 'a - b - b' is a mystery.

    #include <stdio.h>

    int main(void) {
    unsigned long a = 678;
    unsigned long ff = 255;
    unsigned long c = a & ff;

    printf("The value is: %u \n", c);
    ^^^
    %u is the specifier for an unsigned int, not an unsigned long (%lu)

    return 0;

    }
    The value is: 166
    $

    What am I missing here?

    Any understanding of logical operators? See the version below, with a
    note following.

    #include <stdio.h>

    int main(void)
    {
    unsigned a = 678;
    unsigned ff = 255;
    unsigned c = a & ff;
    char format[] = "value of %6s is %#011o (octal), %010u (decimal)\n";

    printf(format, "a", a, a);
    printf(format, "ff", ff, ff);
    printf(format, "a & ff", a & ff, a & ff);
    printf(format, "c", c, c);

    return 0;

    }

    value of a is 00000001246 (octal), 0000000678 (decimal)
    value of ff is 00000000377 (octal), 0000000255 (decimal)
    value of a & ff is 00000000246 (octal), 0000000166 (decimal)
    value of c is 00000000246 (octal), 0000000166 (decimal)

    [note]
    considering the rightmost 4 octal digits
    01 & 00 is binary 001 & 000, obviously 000 (0 octal)
    02 & 03 is binary 010 & 011, obviously 010 (2 octal)
    04 & 07 is binary 100 & 111, obviously 100 (4 octal)
    06 & 07 is binary 110 & 111, obviously 110 (6 octal)

    so 01245 & 0377 is 0246 or 166 decimal.

  • No.8 | | 816 bytes | |

    Chad a :
    For some strange reason, I thought the following would give me a value
    of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.

    #include <stdio.h>

    int main(void) {
    unsigned long a = 678;
    unsigned long ff = 255;
    unsigned long c = a & ff;

    printf("The value is: %u \n", c);

    return 0;

    }
    The value is: 166
    $

    What am I missing here?

    bitwise operators are about bits. Hence you should use the octal or the
    hexadecimal representation that makes it far more clear.

    #include <stdio.h>

    int main(void)
    {
    unsigned long a = 0x2A6;
    unsigned long ff = 0xFF;
    unsigned long c = a & ff; /* I expect 0xA6 */

    printf ("The value is: 0x%X\n", c);

    return 0;
    }

    The value is: 0xA6

    Neat.
  • No.9 | | 817 bytes | |

    Chad a :
    For some strange reason, I thought the following would give me a value
    of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.

    #include <stdio.h>

    int main(void) {
    unsigned long a = 678;
    unsigned long ff = 255;
    unsigned long c = a & ff;

    printf("The value is: %u \n", c);

    return 0;

    }
    The value is: 166
    $

    What am I missing here?

    bitwise operators are about bits. Hence you should use the octal or the
    hexadecimal representation that makes it far more clear.

    #include <stdio.h>

    int main(void)
    {
    unsigned long a = 0x2A6;
    unsigned long ff = 0xFF;
    unsigned long c = a & ff; /* I expect 0xA6 */

    printf ("The value is: 0x%lX\n", c);

    return 0;
    }

    The value is: 0xA6

    Neat.
  • No.10 | | 809 bytes | |

    7 Dec 2005 06:26:33 -0800, "Chad" <cdalten@gmail.comwrote:

    >For some strange reason, I thought the following would give me a value
    >of 168, not 166. My reasoning was that 678 - 255 - 255 = 168.
    >
    >#include <stdio.h>
    >
    >int main(void) {

    unsigned long a = 678;
    unsigned long ff = 255;
    unsigned long c = a & ff;

    printf("The value is: %u \n", c);

    In addition to the other advice, %u requires an unsigned int. You are
    passing an unsigned long. This is undefined behavior.

    return 0;
    >
    >}
    >The value is: 166
    >$
    >
    >What am I missing here?
    >
    >Thanks in advance
    >Chad


    <<Remove the del for email>>

Re: Question about bitwise operators


max 4000 letters.
Your nickname that display:
In order to stop the spam: 2 + 1 =
QUESTION ON "C/C++"

EMSDN.COM