Bit operations, problem converting


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Bit operations, problem converting

  1. #1
    Join Date
    Sep 2004
    Posts
    10

    Bit operations, problem converting

    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

  2. #2
    Join Date
    Sep 2004
    Posts
    150
    You will want to start by looking at the binary representation of the numbers.

    Lets say you had a program that told you what the binary representation of these two numbers would look like.

    As it turns out -24,001 looks like this (when stored on an intel based machine):

    1010 0010 0011 1111

    41,535 looks like this:

    0000 0000 0000 0000
    1010 0010 0011 1111

    This is all one line but I left it lined up like that anyway. The number is twice as long but if you notice they are identical for the last 16 bits which I think your instructors are trying to get across.

    This is because the first number uses a sign bit which doesn't get interpreted as a sign bit if it is just placed into a 32 bit int (ie it is interpreted as 41,535).

    If you were to cast them it wouldn't work because Java would make sure you still have -24,001 by actually interpreting the sign bit as such.

    So your exercise is to pretty much make a 32 bit int take on the 16 bits of the short number to get the value 41,535. I'm in a hurry so I can't write more about it.

    Just off the top of my head you will want to do some bit manipulation or variable manipulation with java that does not involve casting (which I would have to look up since I rarely do that sort of thing). I just wanted to show you how you can see it as a mechanical picture.



  3. #3
    Join Date
    Sep 2004
    Posts
    10
    Thanks, I Figured it out, you just have to use the & manipulator!

  4. #4
    Join Date
    Sep 2004
    Posts
    150
    Ahh.. nice. That makes sense. I suppose you could "or" the short with a 32 bit "0" value for the same effect.

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    quick way:

    int i = myShort & 0xFFFF;

    that's it!
    0xFFFF gives you an int that (in binary) looks like:
    0000 0000 0000 0000 1111 1111 1111 1111

    the short is masked against this, and the leading zeroes destroy the sign bit, while the trailing 1s preserve the actual "number" part of the number. when this is interpreted as a int instead of a short, it comes as 41 thousand something,,

    ---

    incidentally, you cant achieve the same effect by orring with zero.. the calculations in java, take place in int-wide space. any shorts are automatically promoted to ints by casting, and hence you know why this wont work (seeing as you explained why a short cannot be merely cast into an int)

    the following program demonstrates all the concepts i have discussed here:

    Code:
    public class Num{
      //puts "leading zeroes" into a binary number passed as a string
      //actually uses Ohs so you can see the bitwidth of the argument
      public static void prn(String s){
        String temp = "00000000000000000000000000000000000000000000000000000000000000000" + s;
        temp = temp.substring(temp.length()-64);
        System.out.println(temp);
      }
    
      public static void main(String[] argv){
    
        short s = -24001;
        int i = s & 0xFFFF;
        prn(Integer.toBinaryString(s));
        prn(Integer.toBinaryString(0xFFFF));
        prn(Integer.toBinaryString(i));
        System.out.println(s + " bitanded into a int is " + i);
    
        short zero = 0;
        int t = s | zero;
        prn(Integer.toBinaryString(s));
        prn(Integer.toBinaryString(zero));
        prn(Integer.toBinaryString(t));
        System.out.println(s + " bitorred into a int is " + i);
    
    
    
      }
    }
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    if you attempt to change it to

    short t = s | zero;

    you get a possible loss of precision int->short error

    ps; the "000000000...0000000" is NOT meant to be broken by a space.. thats the bulletin board software doing that
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center