bitwise « Integer « Java Data Type Q&A

1. How to use a bitwise operator to pass multiple Integer values into a function for Java?

In application frameworks I keep seeing frameworks that allow you to pass in multiple Int values (generally used in place of an enum) into a function. For example:

public class Example

2. store a string in an int

i try to store a string into an integer as follows: i read the characters of the string and every 4 characters i do this:

val = (int) ch << 24 | (int) ...

3. Why does bitwise AND of two short values result in an int value in Java?

short permissions = 0755;
short requested = 0700;
short result = permissions & requested; 
I get a compiler error:
error possible loss of precision
found   : int
required: short
If I'm not totally wrong the ...

4. Wrapping 4 integers in a 64 bit long - java bitwise

Alright, so I have 4 integers I want to wrap in a long. The 4 integers all contains 3 values, positioned in the first 2 bytes:

{pppppp} represents one ...

5. A question in java.lang.Integer internal code

While looking in the code of the method: Integer.toHexString I found the following code :

public static String toHexString(int i) {
    return toUnsignedString(i, 4);

private static String toUnsignedString(int i, int ...

6. Count bits used in int

If you have the binary number 10110 how can I get it to return 5? e.g a number that tells how many bits are used? There are some likewise examples listed ...

7. 16 bit barrel shift in Java

I'm trying to do a rotate right (barrel shift) on an int in Java, e.g.

Input:  0000 0000 0110 1001
Output: 1000 0000 0011 0100
I know I can do a right shift ...

8. Bitwise manipulation of signed integers

int p= -6969317
int r=0xff & (p>>16);
what "r" will show and how?

9. Bitwise devision? (Changing formats of ints)

I'm using Java. I have an integer where:

Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are blue.
I would like to change this to an int ...

10. Packing 4 bytes into an int

Possible Duplicate:
Convert 4 bytes to int
I'm trying to pack 4 bytes into an int using some of the solutions found here, but it doesn't ...

11. Bitwise operator for simply flipping all bits in an integer?

I have to flip all bits in a binary representation of an integer. Given:

The output should be
What is the bitwise operator to accomplish this when used with an integer? For ...

12. Java get first and last 2 byte from int var

I want convert an int into 2 bytes representing that int. Probably must use bitwise and bit shifting, but I dont know what to do.

int x; /* val to convert */

// ?????????

int ...

13. bitwise addition of two integers

I am having difficulty with bitwise addition of 2 integers without using arithmetic operators. My logic is suppose we have to add 3+3=6 in 2's compliment addition the logic is carry 11 (3)0000 0011 (3)0000 0011 _____________ 0000 0110 (6 ) ------------------------ I have understood the boolean logic. The task is now to write a java program without using any arithmetic ...

14. How do i convert a int to a binary with bitwise operators only?

Are you asking how to get a String representation of the binary value of an int? To do that you need to look at each bit position of an int and test if its a 0 or a 1. Use the AND operator for that. Remember: 1 AND 1 = 1 1 AND 0 = 0 To look at all 32 ...

15. Bitwise shift of short integers

I am not able to do bitwise right shift of short integers in a consistent way. For instance: short bitVal = (short)0xC007; // bitVal == -16377 short shoulBeNegative = 0xC007 >> 2; // with integers the sign bit is preserved not with short!! short shouldBePositive = (short)(bitVal >>> 2); // with integers the sign bit is lost, not so with short!! ...