1

My int values:

    int a= 0x02; int b= 1; int c= 2; int d = 0; int e = 0; int f = 0; int g= 1;

How can I concatenate these integers and get a single int value? and the result int value should have first 8 bits for "a" and 4 bits each for the rest??

Something like : 2120001

6
  • 4
    "Something like : 2120001" Do you expect that result as a decimal number? That contradicts your first sentence where you talk about bits. So what result exactly do you expect? Commented Dec 5, 2012 at 9:25
  • is the data in a possibly occupying 32 bits ? Commented Dec 5, 2012 at 9:25
  • 2
    WHat are you trying to do because I suspect this won't work the way you think it does. For example 0x02 is just 2 and there is no way to determine the number was defined in hex. Commented Dec 5, 2012 at 9:26
  • You don't really "concatenate" ints. You add ints, and concatenate strings. You could convert each of your ints to a string, concatenate them, and convert back to an int, but that result is not even guaranteed to be an int. Commented Dec 5, 2012 at 9:26
  • "a" occupies 8 bits and the rest 4 bit each Commented Dec 5, 2012 at 9:28

7 Answers 7

3
int a= 0x02; int b= 1; int c= 2; int d = 0; int e = 0; int f = 0; int g= 1;
int res=((a&0xff)<<24)|((b&0xf)<<20)|((c&0xf)<<16)
       |((d&0xf)<<12)|((e&0xf)<<8)|((f&0xf)<<4)|((g&0xf));
System.out.println(Integer.toHexString(res));

Yields: 2120001

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't yield 212001. It yields 0x212001. Not the same thing.
3
String yourString = "" + a + b + c + d + e + f + g;
int finalInt = Integer.parseInt(yourString);

6 Comments

Integer.parseInt(yourString, 4);
This will not meet the specs of the question, as far as the first 8 bits, the next 4 bits, etc. For example, the first 8 bits of this resulting int will be 00000000.
0x02 = 2. As per the problem, 0x02,1,2,0,0,0,1 = 212001 which is the poster's desired result.
int a = 0x02; int b = 1; int b = 2; int d = 0; int e = 0; int f = 0; int g = 1; String z = "" + a + b + c + d + e + f + g; int j = Integer.parseInt(z); System.out.println(j);
0x02,1,2,0,0,0,1 = 2120001, not 212001, @MichaelArdan
|
1

Forget

With thanks to @MichaelArdan

String yourString = "" + b + c + d + e + f + g;
int finalInt = Integer.parseInt(yourString, 4) | (a << (6*2));

It interpretes bcdefg as base 4 number, and shifts a to the end.


(Misread the question)

int n = a;
int[] v = new int[] { b, c, d, e, f, g };
int p = 8;
for (int k : v) {
     n |= (k & 0xF) << p;
     p += 4;
}
System.out.println(n);

2 Comments

:D int a = 0x02 would still be considered as 2.
Here, I also don´t like the using-string-concatenation-operator-to-avoid-doing-the-arithmetic-right nature of the answer.
0

How about

String s = "" + a + b + c + d + e + f + g;
int concat = Integer.parseInt(s);

and read up on the Java type conversion and promotions.

3 Comments

what about "result int value should have first 8 bits for the "a" and the 4 bits each for the rest??"
Should convert back "s" it to int.
I don´t like this since you are doing a lot of StringBuilder calls between-the-lines just to avoid some highly efficient bitwise operations like shifts and ors (or adds). A division can be implemented as a series of string manipulations, too, but that would be far from efficient, and far away from the true nature of the operation.
0

You can achieve this by the following way

public static void main(String[] args) throws UnsupportedEncodingException {
        int a = 5;
        int b = 3;
        int bLength = Integer.toBinaryString(b).length();
        System.out.println("a = "  + Integer.toBinaryString(a));
        System.out.println("b = "  + Integer.toBinaryString(b));
        int c = (a << bLength) | b;

        //System.out.println(c);
        System.out.println("c = "  + Integer.toBinaryString(c));


    }

Output

a = 101
b = 11
c = 10111

Comments

0

I think what you want is something like this:

public static void main(String[] args)
    {
        int a=2;
        int b=1;
        int c=2;
        int d=0;
        int e=0;
        int f=0;
        int g=1;

        System.out.println(g+(f << 4)+(e << 8)+(d << 12)+(c << 16)+(b << 20)+(a << 28));
    }

Result is 538050561, which is 20120001 in hex.

See also http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html for bitwise shift operators.

Comments

0

To yield 212001 you need

((((((a*10)+b)*10+c)*10+d)*10+e)*10+f)*10+g

which shows you what's going on, or more simply

a*1000000+b*100000+c*10000+d*1000+e*100+f*10+g

However that contradicts your statement about bit width.

I conclude that what you really want is 0x212001, which is yielded by

(((((((((((a << 4)+b) << 4)+c) << 4)+d) << 4)+e) << 4)+f) << 4)+g

or more simply

(a << 24)|(b << 20)|(c << 16)|(d << 12)|(e << 8)|(f << 8)|g

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.