Skip to Content
Big Java, 4th Edition
book

Big Java, 4th Edition

by Cay Horstmann
December 2009
Intermediate to advanced
1170 pages
28h 4m
English
Wiley
Content preview from Big Java, 4th Edition

Appendix J. Bit and Shift Operations

There are four bit operations in Java: the unary negation (~) and the binary and (&), or (|), and exclusive or (^), often called xor.

Tables 1 and 2 show the truth tables for the bit operations in Java. When a bit operation is applied to integer values, the operation is carried out on corresponding bits.

Table J.1. The Unary Negation Operation

a

~a

0

1

1

0

Table J.2. The Binary And, Or, and Xor Operations

a

b

a & b

a | b

a ^ b

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0

For example, suppose we want to compute 46 & 13. First convert both values to binary. 46decimal = 101110binary (actually 00000000000000000000000000101110 as a 32-bit integer), and 13decimal = 1101binary. Now combine corresponding bits:

The Binary And, Or, and Xor Operations

The answer is 1100binary = 12decimal.

You sometimes see the | operator being used to combine two bit patterns. For example, Font.BOLD is the value 1, Font.ITALIC is 2. The binary or combination Font.BOLD | Font.ITALIC has both the bold and the italic bit set:

The Binary And, Or, and Xor Operations

Don't confuse the & and | bit operators with the && and || operators. The latter work only on boolean values, not on bits of numbers.

Besides the operations that work on individual bits, there are three shift operations that take the bit pattern of a number and shift it to the left or right by a given number of positions. There are three ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Java, 4th Edition

Learning Java, 4th Edition

Patrick Niemeyer, Daniel Leuck
Think Java, 2nd Edition

Think Java, 2nd Edition

Allen B. Downey, Chris Mayfield
Learning Java, 5th Edition

Learning Java, 5th Edition

Marc Loy, Patrick Niemeyer, Daniel Leuck
Java in a Nutshell, 8th Edition

Java in a Nutshell, 8th Edition

Benjamin J. Evans, Jason R. Clark, David Flanagan

Publisher Resources

ISBN: 9780470509487Purchase book