473,231 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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 4628
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
11
by: Randell D. | last post by:
Why would one use bitwise operators? I can program in various languages in some shape or form (C++, PHP, some scripting) and I've heard/seen bitwise operators before, but never understood why...
4
by: Mike Hodkin | last post by:
As a beginning student of C++, books reference "bitwise operators" and give brief examples, but I have not read a good explanation of what they are used for. One reference mentioned that they are...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
37
by: James Radke | last post by:
Hello, I found some code that I could use in my application on the web, but it is written in C#, and I would like to convert it to VB. And I am having problems with one section. Is there...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
17
by: Dinsdale | last post by:
I would like to compare a string value to a pre-determined list of other strings. Is there a simple way to do this in one statements like this: if(strMystring.ToUpper() == ("STRING1"| "STRING2"|...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
39
by: Boltar | last post by:
Why does C do lazy evaluation for logical boolean operations but not bitwise ones? Ie: the following program prints "1 2" , not "1 1" under gcc main() { int a = 1; int b = 1; 0 && ++a;
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.