468,556 Members | 2,192 Online

# Question about bitwise operators

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

Dec 7 '05 #1
10 4494 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

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

Dec 7 '05 #4

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

--
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
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

--
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.

Chad

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

--
pete

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

678 = 1010100110
256 = 0100000000
& = 0000000000 = 0

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

Dec 7 '05 #8
Chad a écrit :
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
Chad a écrit :
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?

Thanks in advance
Chad

<<Remove the del for email>>
Dec 18 '05 #11

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 9 posts views Thread by Michael B. Trausch | last post: by 11 posts views Thread by Randell D. | last post: by 4 posts views Thread by Mike Hodkin | last post: by 6 posts views Thread by jas_lx | last post: by 2 posts views Thread by Steve Summit | last post: by 37 posts views Thread by James Radke | last post: by 5 posts views Thread by noridotjabi | last post: by 17 posts views Thread by Dinsdale | last post: by 29 posts views Thread by Carl Banks | last post: by 39 posts views Thread by Boltar | last post: by reply views Thread by NPC403 | last post: by reply views Thread by slotstar | last post: by 7 posts views Thread by isladogs | last post: by reply views Thread by captainhaddock | last post: by 3 posts views Thread by hwsilver | last post: by reply views Thread by yuyenews | last post: by 3 posts views Thread by laxmi96 | last post: by 7 posts views Thread by Barbara1999 | last post: by 1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.