Everything2
Near Matches
Ignore Exact
Full Text
Everything2

bitwise

created by wharfinger

(idea) by wharfinger (6.3 y) (print)   ?   (I like it!) 1 C! Mon Apr 03 2000 at 0:40:45

Bitwise operations are one of the groovier features of C, C++, Java, and probably a dozen other languages.

Everything in a computer is just a long, long list of zeroes and ones. Ordinarily, you can only alter the bits one byte (eight bits) at a time (or two bytes at a time, or four, or nowadays eight). Bitwise operations let you mess with the individual bits themselves. It's fun, and it goes like this: You beat two integers against each other, and...

  • Bitwise OR: "|" If a given bit is "on" in either operand, or in both operands, the corresponding bit in the result will be "on":

    5 | 9 == 13
    
      0101    ==  5
    | 1001    ==  9
    ------ 
      1101    == 13
    
  • Bitwise AND: "&" If and only if a given bit is "on" in both operands, the corresponding bit in the result will be "on":

    5 & 9 == 1
      
      0101    == 5 
    & 1001    == 9 
    ------ 
      0001    == 1 
    
  • Bitwise exclusive OR (XOR): "^" If a given bit is "on" in one operand and not the other, the corresponding bit in the result will be "on":

    5 ^ 9 == 12
    
      0101    == 5
    ^ 1001    == 9
    ------
      1100    == 12
    
  • Bitwise complement: "~" This is an unary operator, which is to say that it takes only one operand. If a given bit in that one lonesome operand is "on", the corresponding bit in the result will be "off", and vice-versa (see also Obo):

    ~9 == 6
    
    ~ 1001    == 9
    ------ 
      0110    == 6
    

This is probably not as clear as it could be, but if you're trying to learn C from E2, umm, I got news for ya...




We thank Jamer for spotting a gruesome error in the XOR example.


printable version
chaos

Obo bitwise XOR 192.168.0.0/16 ALU
bitand <=> | &
Game theory counting 1 bits SPOILER Creating a Commodore 64 character set C
mask Reading the Commodore 64 joystick Odd unary
Boolean in BASIC Misdight bit twiddling Exclusive Or
net mask Windows calculator logical operators and
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Nodes your grandpa would have liked:
Flaws with Pascal's Wager
More 9/11 Conspiracy Theories
I know they are watching me
Andrew Jackson
common law
Hugh Hefner
The Vagina Monologues
ekpyrotic universe
Chicken pox
X-Men
watermelon
Peter Pan
Shanghainese
New Writeups
tentative
Chances Not Taken(idea)
Heitah
Why I love Everything2(person)
trixingee
Dungeon Mastering for the first time(idea)
Netrat0
It's Called Subtext, Honey(person)
eyeofthebeholder
The Dragon(idea)
Heitah
consist, comprise, constitute, or compose(idea)
Meezzio
Gotlandssnus(thing)
argv
Astral Plane(idea)
Madara
One Winged Angel(fiction)
Tom Rook
Talk is cheap(poetry)
shaogo
Adelle Davis(person)
Aerobe
race car g sfjsgsd(poetry)
Binah
Dream Log: July 5, 2008(dream)
StrawberryFrog
Forgotten things in space(idea)
antigravpussy
velvet revolution fairy tale(idea)
This page courtesy of The Everything Development Company