Question about bitwise operators

C

Chad

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
 
P

pete

Chad said:
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
 
S

Skarmander

Chad said:
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.
 
C

Carl R. Davies

Chad said:
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.
 
C

Chad

pete said:
Try it with 256 instead.

678 is 0x2a6

0xa6 is 166

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

Chad

Chad said:
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
 
M

Martin Ambuhl

Chad said:
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.
 
E

Emmanuel Delahaye

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

Emmanuel Delahaye

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

Barry Schwarz

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,473
Latest member
ChristelPe

Latest Threads

Top