C: Ampersand In Front Of A Number - Stack Overflow

Có thể bạn quan tâm

Just browsing Stack Overflow? Help us improve your experience. Sign up for research
    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs C: Ampersand in front of a number Ask Question Asked 12 years, 8 months ago Modified 12 years, 8 months ago Viewed 2k times 0

What does this line (x = n & 5;) mean in the code below? As far as I know ampersand is used for pointers. I was not expecting this code to compiled, but it compiled and ran fine. The results I got is

0,1,0,1,4,5,4,5,0,1,

#include <stdio.h> int main(void){ int x, n; for (n = 0; n < 10; n++){ x = n & 5; printf("%d,", x); } printf("\n"); return 0; } Share Improve this question Follow edited Apr 5, 2012 at 0:08 Jesse Good's user avatar Jesse Good 52.3k14 gold badges129 silver badges173 bronze badges asked Apr 5, 2012 at 0:06 lamba's user avatar lambalamba 1,6415 gold badges19 silver badges29 bronze badges 3
  • I compiled using gcc -W -Wall -pedantic -std=c89 ctest.c – lamba Commented Apr 5, 2012 at 0:07
  • 3 It's an operator. Open your introductory C++ textbook and read it until you're familiar with the language basics. – Kerrek SB Commented Apr 5, 2012 at 0:08
  • Sorry, I had a long day. Can't believe I forgot about it. I even used it before. – lamba Commented Apr 5, 2012 at 0:10
Add a comment |

4 Answers 4

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 6

In this case it's bitwise AND.

x = n & 5;

will AND 5 (which is 0b101) with whatever is in n.

AND works on the bits that represent the values. The result will have a 1 if both values have a 1 in that position, 0 otherwise.

Since we're ANDing with 5, and there are only 4 values you can make with the two bits in 0b101, the only possible values for x are 1, 4, 5 and 0. Here's an illustration:

n & 5 = x 1 (0b0001) & 0b0101 = 1 (0b0001) 2 (0b0010) & 0b0101 = 0 (0b0000) 3 (0b0011) & 0b0101 = 1 (0b0001) 4 (0b0100) & 0b0101 = 4 (0b0100) 5 (0b0101) & 0b0101 = 5 (0b0101) 6 (0b0110) & 0b0101 = 4 (0b0100) 7 (0b0111) & 0b0101 = 4 (0b0100) 8 (0b1000) & 0b0101 = 0 (0b0000) 9 (0b1001) & 0b0101 = 1 (0b0001) Share Improve this answer Follow edited Apr 5, 2012 at 0:15 answered Apr 5, 2012 at 0:07 Timothy Jones's user avatar Timothy JonesTimothy Jones 22.1k6 gold badges64 silver badges94 bronze badges Add a comment | 5

That's the "bitwise and" operator. Normally, it takes two integers, and returns an integer that has only the bits set that are in both of it's parameters.

base10 base2 6 0110 3 0011 6&3 0010 (=2)

There's also "bitwise or" | which sets bits that either one has set.

base10 base2 6 0110 3 0011 6|3 0111 (=7)

and there's "bitwise xor" ^ which sets bits that are different.

base10 base2 6 0110 3 0011 6^3 0101 (=5) Share Improve this answer Follow answered Apr 5, 2012 at 0:12 Mooing Duck's user avatar Mooing DuckMooing Duck 66.6k19 gold badges103 silver badges165 bronze badges 3
  • I think you mean 6&3, 6|3 and 6^3 – torrential coding Commented Apr 5, 2012 at 0:14
  • @torrentialcoding: Thanks. I started with 5 and 2, but then switched to more interesting numbers – Mooing Duck Commented Apr 5, 2012 at 0:16
  • Right. 6 and 3 better illustrate the bit operations. – torrential coding Commented Apr 5, 2012 at 0:19
Add a comment | 2

It's a bitwise AND operator. The value of n is being ANDed with 5.

Share Improve this answer Follow answered Apr 5, 2012 at 0:07 torrential coding's user avatar torrential codingtorrential coding 1,7652 gold badges24 silver badges34 bronze badges 1
  • 2 It's bitwise, not logical operator. – Cat Plus Plus Commented Apr 5, 2012 at 0:07
Add a comment | 2

You were already told that in this case it's a binary and. But this requires a bit more explanation. In C and other C-like languages there are two & operators. One is unary the other binary.

A unary operator acts on a single value alone, while a binary operator takes in two values. In C binary operators take precedence over unary ones. If you write

int b; int *a = &b;

Then b is the only value the operator can work on. If you write however

int c, d; int d = c & d;

then the operator has two values to work with and the binary interpretation takes precedence over the unary interpretation. Note that this does not only apply to the & operator, but also its counterpart *

int *f; int h = *f;

But also

int i,j; int k = i * j;

Like with all operators, precedence can be overridden with parentheses:

int l, *m; int n = l * (*m); Share Improve this answer Follow answered Apr 5, 2012 at 0:15 datenwolf's user avatar datenwolfdatenwolf 162k13 gold badges190 silver badges310 bronze badges Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • Your docs are your infrastructure
  • Featured on Meta
  • More network sites to see advertising test [updated with phase 2]
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...
0 ampersand C reference like operator in java. meaning? 0 The meaning of the ampersand 0 What do I need to put before the ampersand? 1 What does ampersand mean here? 1 What is the mean of the ampersand symbol(&) in this sentence? 0 The use of && in C 0 Ampersand '&' adds unnecessary characters 0 Confusion with C syntax (relating to pointer and ampersand) 0 Not sure about & operator 1 How to escape ampersand character in C programming?

Hot Network Questions

  • It will not last
  • Trump's tariff plan
  • Do I need Letter of invitation to Iceland?
  • Angular orientation of exact solution of the Hydrogen Schrödinger Equation
  • The British used to (still?) classify their guns by weight in pounds rather than caliber. Was that a constant across shell types?
  • What mechanism could cause a person not to cast a reflection?
  • If "de-" means "down" or "off" and "sub-" means "under", what is the latin prefix- meaning "up"?
  • Comedy/Sci-Fi movie about one of the last men on Earth living in a museum/zoo on display for humanoid robots
  • Skylab's Boom during EVA
  • What's a good way to append a nonce to ciphertext in Python for AES GCM in Python?
  • How much ebikes actually benefit from ebike specific wheels, tires, and forks?
  • Sorites paradox and emergence
  • Shell Script to Normalize the data
  • What happens when a ranger uses Favored Foe with Hunter's Mark?
  • Clarification and Proof of Inequality (8.11) in Analytic Number Theory by Iwaniec and Kowalski
  • What exactly is the cornerstone that Mark 12:10 speaks of?
  • Mega Man: Powered Up
  • Is there a cryptographic method that can only decrypt for a certain range of seeds?
  • How can I avoid overusing her/she or the character name when describing character action
  • In GR, what is Gravity? A force or curvature of spacetime?
  • Can this strong directional blur at wide apertures still be explained by the usual arguments?
  • Can you please advise on kerning?
  • Does length contraction "break the speed limit"?
  • How do you calculate the number of skid patches on a fixie?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-c

Từ khóa » C 0b0001