Results 1 to 10 of 10

Thread: bit operations

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Resolved bit operations

    Hello everyone, anyone know how to produce a 32 bit integer from a 16 bit short number?

    Like say I have,

    16-bit short number whose value is equal to -24001. How would i convert this short number to an int k, so that the output of k is equal to 41535.

    This is the first time I ever messed with bit shifting, so i'm not sure how this is suppose to look. I know I can use & to change the 1's to 0's. But other then that i'm pretty confused.


    Thanks for listening!
    Last edited by Dilenger4; Oct 7th, 2004 at 05:08 PM.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I was never that good at bit manipulations but ill try to help anywway.
    You can produce a 32 bit number from a 16 bit number by using the shift operators since byte short and char operands are always promoted to int when applying them. I would think that you would need to apply the right shift unsinged >>> to get a postive value from -24001. Shifting the value 17 and 16 times puts you in range. After that you might have to do some more bit manipulation.
    Code:
    short s = -24001; 
    int i = s >>> 17;  // 17 produces 32767 16 produces 65535

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Just so I get this right - you want the int that represents the value the short would have if it was unsigned and had the same bit pattern?
    Seems like it.

    Basically, to convert a short to an int while pretending that the short is unsigned, you don't have to shift anything. Rather, you do some bit clearing.
    Code:
    int asUnsignedToInt(short s) {
      return (((int)s) & 0x0000FFFF);
    }
    I probably should explain.
    This is the bit pattern of both -24001 and 41535 as a 16-bit signed int:
    1010001000111111
    The pattern of 41535 in 32 bit is this:
    0000000000000000 1010001000111111
    and of -24001:
    1111111111111111 1010001000111111

    As you can see, they only differ in that in one, the HOW is completely set, in the other it's completely cleared.
    Since (int)-24001 results in the lower pattern, but you want the upper, we have to clear all those upper bits. That's what the code does.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Interesting how the most significant bits of -24001 and 41535 are the same.
    Code:
       int i = 24001;
       String bin = Integer.toBinaryString(i);
       System.out.println(i);
       System.out.println("00000000000000000" + bin);
       i = ~i + 1;
       String bin2 = Integer.toBinaryString(i); 
       System.out.println(i);
       System.out.println(bin2);
       int i2 = 41535;
       String bin3 = Integer.toBinaryString(i2); 
       System.out.println(i2);
       System.out.println("0000000000000000" + bin3);

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    How do you get 41535 back to -24001? If 41535 has the bit pattern 0000000000000000 1010001000111111 then you would have to add bits not clear right? So you would have to get somthing with the bit pattern 11111111 11111111 then apply apply the | operator on the most significant 16 bits?

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    short toShortByPattern(int i) {
      if(i > 0x0000FFFF || i < 0) {
        throw new IllegalArgumentException("i too large");
      }
      if(i > ((int)Short.MAX_VALUE)) {
        i |= 0xFFFF0000;
      }
      return (short)i;
    }
    Values > 0x0000FFFF would produce incorrect behaviour and are thus rejected.
    Since values between Short.MAX_VALUE (0x00007FFF) and 0x0000FFFF have bit patterns that are interpreted as negative when it's an actual short, we need to convince the VM to convert as if it was a negative number. We do this by setting all the upper bits to 1. Thus the integer suddenly has the same (negative) value that a short with the same pattern would have.
    Then, or if i was in the domain of a positive signed short in the first place, we can safely do the cast to short and get a correct result.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Funny i didn't even read your post.
    Code:
      int i = -24001;
      System.out.println(i);    
      String bin = Integer.toBinaryString(i);
      System.out.println(bin);
      i = i & 0x0000ffff;
      System.out.println(i); // 41535  
      String bin2 = Integer.toBinaryString(i);
      System.out.println("0000000000000000" + bin2);
      i = i | 0xffff0000;
      System.out.println(i); //-24001
      String bin3 = Integer.toBinaryString(i);   
      System.out.println(bin3);

  8. #8

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    thanks

    Hey thanks guys for the post, But when i ran the code, I got a different output, any ideas why?

    Code:
    public class Convert {
      public Convert() {
      }
      int asUnsignedToInt(short s) {
      return (((int)s) & 0x0000FFFF);
    }
    
    public static void main(String args[])
    {
      Convert c = new Convert();
      System.out.println(c.asUnsignedToInt((short)-2400));
    }
    
    }
    output:
    63136


    Thanks!
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  9. #9
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Posted by voidflux

    Hey thanks guys for the post, But when i ran the code, I got a different output, any ideas why? 63136

    public class Convert {
    public Convert() {
    }
    int asUnsignedToInt(short s) {
    return (((int)s) & 0x0000FFFF);
    }
    public static void main(String args[]){
    Convert c = new Convert();
    System.out.println(c.asUnsignedToInt((short)-2400));
    }
    }
    That's becasue you are supposed to pass -24001 not -2400

  10. #10

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    thanks again guys, its been a long night, i don't know why I thought I put in the right value
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width