Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a bitwise & inside an if statement

In C, I can write an if-statement

if (firstInt & 1)

but when I try and do the same in Java, the compiler tells me "incompatible types" and says I need a boolean instead of an int. Is there any way to write that C code in Java?

like image 523
user1310650 Avatar asked Apr 08 '12 15:04

user1310650


People also ask

Why use a bitwise operators?

Because they allow greater precision and require fewer resources, bitwise operators can make some code faster and more efficient. Examples of uses of bitwise operations include encryption, compression, graphics, communications over ports/sockets, embedded systems programming and finite state machines.

What bitwise means?

Bitwise is a level of operation that involves working with individual bits which are the smallest units of data in a computing system. Each bit has single binary value of 0 or 1. Most programming languages manipulate groups of 8, 16 or 32 bits. These bit multiples are known as bytes.

How do you solve bitwise problems?

Find the two non-repeating elements in an array of repeating elements. Write an Efficient C Program to Reverse Bits of a Number. Smallest power of 2 greater than or equal to n. Write a C program to find the parity of an unsigned integer.


1 Answers

Any of the following should work for you:

if ((firstInt & 1) != 0)
if ((firstInt & 1) > 0)
if ((firstInt & 1) == 1)
like image 119
Tim Cooper Avatar answered Nov 15 '22 19:11

Tim Cooper