470,862 Members | 1,625 Online

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?

Dec 7 '05 #1
10 4554

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 is 0x2a6

0xa6 is 166

--
pete
Dec 7 '05 #2
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.
Dec 7 '05 #3
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.

Dec 7 '05 #4

pete 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 is 0x2a6

0xa6 is 166

--
pete

when I change the value of ff to 256, I get zero.

#include <stdio.h>

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

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

return 0;

}

The value is: 0
\$

Dec 7 '05 #5
pete 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 is 0x2a6

0xa6 is 166

--
pete

when I change the value of ff to 256, I get zero.

#include <stdio.h>

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

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

return 0;

}

The value is: 0
\$

Okay, wait, never mind. I just read the rest of the threads and thought
about it for a second. I get the correct values. Thanks.

Dec 7 '05 #6
pete 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 is 0x2a6

0xa6 is 166

--
pete

when I change the value of ff to 256, I get zero.

678 = 1010100110
256 = 0100000000
& = 0000000000 = 0

Dec 7 '05 #7
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.

Dec 7 '05 #8
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.

--
A+

Emmanuel Delahaye
Dec 7 '05 #9
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.

--
A+

Emmanuel Delahaye
Dec 7 '05 #10
On 7 Dec 2005 06:26:33 -0800, "Chad" <cd*****@gmail.com> 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;

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?