473,386 Members | 2,042 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,386 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 4633
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.