Bit-Shifting vs. Math Ops

I have always been under the impression that doing a bit shift is faster than I have always been under the impression that doing a bit shift is faster than performing the equivalent math operation when the math involves multiplying or dividing by a positive power of two. For instance, performing the following math op
int i = 8;
i = i << 3;
System.out.println(i); //prints out the number 64should be faster than
int i = 8;
i = i * 8;
System.out.println(i); //prints out the number 64The same applies when doing integer division (i.e. that a shift with zero fill to the right would be faster than performing the division operator).
I have two questions:
(1) Is there really a performance benefit to doing bit-shifting when it comes to integer math operations involving powers of two over using the regular multiplication and division operators?
(2) If I perform the integer math ops using bit-shifting instead of using the traditional operators, how cross-platform is this kind of math? On my Windows machine, which apparently uses little Endian representation, the shifts perform as expected (i.e. << performs multiplication and >>> performs division on positive integer powers of two). However, if I run my code on a platform that encodes integers using big Endian, will the operations still perform as I expect, or am I unwittingly making my code platform-dependent?
I appreciate any help you can offer me on this matter.
-Dok

Bit shifting seems to be used on integer operations with powers of two. Multiply or divide by 8 is being ran in the same amount of time as a bit shift of 3. However, if I change the 8 to a non-power of two like 7, then it takes longer. So these are optimizations being made by the compiler. However, these don't seem to apply to longs, and a long divide by 8 takes 4-5 times longer then a shift of 3.
Also, I may be wrong, but I don't think you have to worry about a little Endian/big Endian representation. Bitwise operators seem to work fine on either platform (at least I haven't run into problems running them on multiple platforms).

Similar Messages

  • Issue with bit shifting

    Hi everyone!
    I have a question about bit shifting. When I attempt to shift 32 bit on a int value, the output is the same value. It doesn't matter if I try to make a rigth or left shifting. It also hapend when I make 64 bit shifting on a long value. I supposed it should be 0. This is the code I'm woking with:
    int x = 2147483647;
    System.out.println("Output1 = " + (x >> 32));
    System.out.println("Output2 = " + (x << 32));
    Output:
    Output1 = 2147483647
    Output2 = 2147483647
    Could someone help me to understand why this happend?
    Thanks in advance.

    GerSua wrote:
    Hi everyone!
    I have a question about bit shifting. When I attempt to shift 32 bit on a int value, the output is the same value. It doesn't matter if I try to make a rigth or left shifting. It also hapend when I make 64 bit shifting on a long value. I supposed it should be 0. This is the code I'm woking with:
    int x = 2147483647;
    System.out.println("Output1 = " + (x >> 32));
    System.out.println("Output2 = " + (x << 32));
    Output:
    Output1 = 2147483647
    Output2 = 2147483647
    Could someone help me to understand why this happend?
    Thanks in advance.It is supposed to work this way. I know that's not alot of info, but you have not discovered a bug or anything like that.

  • Bit Shifting Help

    Hi,
    I'm not too familiar with bit shifting. If anyone can help with this problem, it would be great!
    We have words that are 32 bits. So, basically, the lower 8 bits should have value �1� and the upper 24 bits should have value �X�. For our example, lets say 5. How do you manipulate integers to get this format? So, I assume it will look like below.
    31 87 0
    00000000010100000001

    int i = 0x12345678;
    byte highOrder = (byte)(i >> 24);
    byte second = (byte)(i >> 16);
    byte third = (byte)(i >> 8);
    byte lowOrder = (byte)i;

  • Problems with bit shift left in Formula Node

    Hi,
    Sorry but my English is not good.
    I'm trying to execute a bit shift left in the Formula Node, but the shift left is not working as I expected.
    uInt32 parametro[5];
    float32 valorAmostra;
    int16 indiceAmostra;
    uInt8 indiceResposta;
    int16 controle;
    int32 valor;
    int8 i;
    for(i=0; i<5; i++)
    parametro[i]=param[i];
    valor = parametro[5]<<16 | parametro[4]<<8 | parametro[3];
    controle = parametro[1]<<8 | parametro[0];
    indiceAmostra = (controle & 0x3FF);
    indiceResposta = (controle >> 10);
    valorAmostra = valor & 0x7FFFFF;
    valorAmostra = valorAmostra*(2.4/8388607);
    if((valor & 0x800000) == 0x800000)
    valorAmostra = valorAmostra*(-1);
    the input param array is of the type uInt8, as I saw in other topics that does not shift with uInt8,
    I copy the values of param array to parametro which is of type  uInt32, but still did not work.
    Testing the input param with the following values:
    param[] = {10,14,10,0,16}
    the valorAmostra returned is 0,00117188 when he should be 0,300003.
    Can someone help me please?
    Solved!
    Go to Solution.

    Portuguese:
    Olá,
    Existe um bug no LabVIEW para esta operação. Este bug deverá ser corrigido em versões futuras. Uma solução para esta situação é utilizar um int32 ao invés de um ulnt32.
    Qualquer dúvida por favor entre em contato.
    Obrigado.
    English:
    I investigated the problem you are having, and it turns out that it is a bug that has already been reported and documented. The work around for it is to use int32 rather than uInt32.
    Thanks.
    Luciano Borges
    R&D Engineer
    Pirelli Brazil

  • IMAQ Array to Image Bit Shift?

    Hi,
    I've been working with some 12 bit images from a camera and saving the results as 16 bit TIFFs using some of the Vision VIs.  I can save and read back in the TIFFs and all of the data stays intact, but if I try to open the saved TIFFs in another reader (e.g. MATLAB), the returned data is incorrect.  Opening the raw TIFF files and looking at the image data (little endian) in bytes, numbers like 234 001 [490] end up becoming 234 129 [33258].  From the attached VI, it becomes clear there's a problem when you simply put in zeros to the array and try to open up the resultant TIFF in MATLAB (0 goes to 2^15) and yet LabView reads in the TIFF just fine.  While I can work around this problem, I only know I that need to work around it if I know a priori that the TIFF is from LabView.  Is there some resolution to this problem?  I'm thinking some kind of bit shifting to the data in LabView before converting the array to an image.  Thanks.
    Attachments:
    array_to_tiff.vi ‏49 KB

    Hello,
    Maybe you are looking for the functions "swap byte" and "swap words"
    Hope this helps
    Message Edité par TiTou le 06-29-2007 02:00 PM
    When my feet touch the ground each morning the devil thinks "bloody hell... He's up again!"
    Attachments:
    swap.png ‏409 KB

  • Large scale bit-shifting

    Are there any libraries or reccomended techniques foir large-scale bit-shifting.
    I have a byte array (about 100KB). In the middle of the array, I need to insert 13 bits, and shift everyhting after those bits. The file format is not byte-aligned.
    Is there a "good" way to do this?
    Thanks,
    -Micah

    mtf wrote:
    Are there any libraries or reccomended techniques foir large-scale bit-shifting.
    I have a byte array (about 100KB). In the middle of the array, I need to insert 13 bits, and shift everyhting after those bits. The file format is not byte-aligned.
    Is there a "good" way to do this?I don't know of a library to do this, but it's pretty straightforward to implement, so long as you're clear about which end of the byte you consider to be the beginning (i.e., does the bit stream begin with bit 0 or bit 7 of a byte).

  • Expression node bit shift

    Is this a bug or am I just thinking about this wrong?  The attached VI shows a U8 going through an expression node where the expression is "x << y".  I've tried multiple versions of y and as soon as y becomes large enough to shift all the original 1's off the U8, the result becomes 0xFF instead of 0x00.  I would assume that its intent was to match C's functionality since it matches C's syntax but this does not appear to be the case.
    Any suggestions or is this a bug?
    Thanks
    Andy Brown
    Averna
    Attachments:
    expression_shift_issue.vi ‏11 KB

    Hey Andy,
    Thank you for contacting National Instruments.  It does look like that is acting strangely.  However, we do have a Logical Shift VI that I believe accomplishes what you are looking for.
    On the Block Diagram, go to Programming>>Numeric>>Data Manipulation>>Logical Shift.
    It takes two inputs, x and y, with x being the original number, and y being the number of bits you want to shift.  It looks like it will clear out to 00 once all the bits have been shifted over.
    Let me know if you any questions!
    Regards,
    Kevin H
    National Instruments
    WSN/Wireless DAQ Product Support Engineer

  • Bit shifting tricks and speeding up data structure

    I'm pretty new to working directly with bits, but it is a lot of fun. I'm converting a javascript data structure to Flash.
    So I'm thinking that Math.floor(uint/32) is the same as uint >> 5, right?
    And that uint % 32 is the same as uint & 31 ? Evidently this works for modulos that are powers of 2, what a cool trick.
    Using both of those cut the time needed for storing and retrieving data about in half.
    But it is still takes about three times longer that just using the native Object. This is in reference to my previous post http://forums.adobe.com/message/5001353#5001353 but that didn't get very far.
    So if anybody has any suggestions on how else to speed this up it would be very helpful:
    package {
              public class BloomFilter {
                        private var _m:uint;
                        private var _k:int;
                        private var _locations:Vector.<uint>;
                        public var _buckets:Vector.<uint>;
                        public function BloomFilter(m:uint,k:int) {
                                  _m=m;
                                  _k=k;
                                  _locations=new Vector.<uint>  ;
                                  _buckets=new Vector.<uint>  ;
                                  var n:uint=Math.ceil(m/32);
                                  var i:int=-1;
                                  while (++i<n) {
                                            _buckets[i]=0;
                        private function hash(v:String) {
                                  var i:int=-1;
                                  var X:uint;
                                  var a,b:uint;
                                  a=fnv_1a(v);
                                  b=fnv_1a_b(a);
                                  X=a%_m;
                                  while (++i<_k) {
                                            _locations[i]=X<0? (X+_m) : X;
                                            X=(X+b)%_m;
                        public function add(v:String) {
                                  hash(v);
                                  var i:int=-1;
                                  while (++i<_k) {
                                            _buckets[_locations[i]>>5]|=1<<(_locations[i]&31);
                        public function test(v:String):Boolean {
                                  hash(v);
                                  var i:int=-1;
                                  var b:uint;
                                  while (++i<_k) {
                                            b=_locations[i];
                                             if ((_buckets[b>>5] & (1 << (b&31))) === 0) {
                                                      return false;
                                  return true;
                        // Fowler-Noll-Vo hash
                        private function fnv_1a(v:String):uint {
                                  var n:int=v.length;
                                  var a:uint=2166136261;
                                  var c,d:uint;
                                  var i:int=-1;
                                  while (++i<n) {
                                            c=v.charCodeAt(i);
                                            if (d=c&0xff000000) {
                                                      a^=d>>24;
                                                      a+=(a<<1) + (a<<4) + (a<<7) + (a<<8)+ (a<<24);
                                            if (d=c&0xff0000) {
                                                      a^=d>>16;
                                                      a+=(a<<1) + (a<<4) + (a<<7) + (a<<8)+ (a<<24);
                                            if (d=c&0xff00) {
                                                      a^=d>>8;
                                                      a+=(a<<1) + (a<<4) + (a<<7) + (a<<8)+ (a<<24);
                                            a^=c&0xff;
                                            a+=(a<<1) + (a<<4) + (a<<7) + (a<<8)+ (a<<24);
                                  a+=a<<13;
                                  a^=a>>7;
                                  a+=a<<3;
                                  a^=a>>17;
                                  a+=a<<5;
                                  return a&0xffffffff;
                        // One additional iteration of FNV
                        private function fnv_1a_b(a:uint):uint {
                                  a+=(a<<1) + (a<<4) + (a<<7) + (a<<8)+ (a<<24);
                                  a+=a<<13;
                                  a^=a>>7;
                                  a+=a<<3;
                                  a^=a>>17;
                                  a+=a<<5;
                                  return a&0xffffffff;

    the math shortcuts, as mentioned above, have a greater benefit.  (and thanks.  yes, that's from a book i wrote,
    Flash Game Development: In A Social, Mobile and 3D World
    Math Class Shortcuts
    These methods actually do provide a benefit and that benefit can be significant even without needing millions of operations to realize. They are listed in alphabetical order starting with Math.abs. (See support files/Chapter 07/math_class/Math.abs_v_conditional.fla, support files/Chapter 07/math_class/Math.floor_v_int.fla etc.)
    Math.abs
    Using
    x = (y<0) ? -y: y;
    instead of
    x = Math.abs(y)
    is about twice as fast. Unless you are using millions of Math.abs operations, you should not expect a noticeable benefit from using the inline code. In addition, the inline code is cumbersome.
    var xx:int;
    var n:int = 10000000;
    var n2:int = n/2;
    var i:int;
    var startTime:int = getTimer();
    //// Math.abs duration: 1016
    for(i=0;i<n;i++){
           xx = Math.abs(n2-i);
    trace("Math.abs duration:",getTimer()-startTime);
    // conditional duration: 445
    startTime = getTimer();
    for(i=0;i<n;i++){
            xx = (n2-i<0) ? i-n2 : n2-i;
    trace("conditional duration:",getTimer()-startTime);
    Math.ceil and Math.floor
    Using
    x = int(y);
    Instead of
    x = Math.floor(y); // y>=0
    x = Math.ceil(y); // y<=0
    Is about twice as fast. Unless you are using millions of Math.floor operations (with non-negative numbers), you should not expect a noticeable benefit.
    var i:int;
    var n:int = 10000000;
    var xx:int;
    var startTime:int = getTimer();
    // Math.floor duration: 1105
    for(i=0;i<n;i++){
           xx = Math.floor(i/n);
    trace("Math.floor duration:",getTimer()-startTime);
    // int duration: 479
    startTime = getTimer();
    for(i=0;i<n;i++){
           xx = int(i/n);
    trace("int duration:",getTimer()-startTime);
    Math.max
    Using
    x = (i>j) ? i : j;
    instead of
    x = Math.max(i,j);
    is about twice as fast.
    This shortcut is also cumbersome but has the greatest benefit (along with Math.min) of all those listed in this section. Notice the difference in time required to execute the code blocks and how few iterations are needed to demonstrate that difference.
    var xx:int;
    var n:int = 1000;
    var i:int;
    var j:int;
    var startTime:int;
    // Math.max duration: 109
    startTime = getTimer();
    for(i=n-1;i>=0;i--){
           for(j=n-1;j>-0;j--){
                  xx = Math.max(i,j);
    trace("Math.max duration:",getTimer()-startTime);
    // conditional duration 43
    startTime = getTimer();
    for(i=n-1;i>=0;i--){
           for(j=n-1;j>-0;j--){
                   xx = (i>j) ? i : j;
    trace("conditional duration",getTimer()-startTime);
    Math.min
    Using
    x = (i<j) ? i : j;
    instead of
    x = Math.min(i,j);
    is about twice as fast.
    This shortcut is also cumbersome but has the greatest benefit (along with Math.max) of all those listed in this section. Notice the difference in time required to execute the code blocks and how few iterations are needed to demonstrate that difference.
    var xx:int;
    var n:int = 1000;
    var i:int;
    var j:int;
    var startTime:int;
    // Duration Math.min 121
    startTime = getTimer();
    for(i=0;i<n;i++){
           for(j=0;j<n;j++){
                  xx = Math.min(i,j);
    trace("Duration Math.min",getTimer()-startTime);
    // Duration conditional 43
    startTime = getTimer();
    for(i=0;i<n;i++){
           for(j=0;j<n;j++){
                   xx = (i<j) ? i : j;
    trace("Duration conditional",getTimer()-startTime);
    Math.pow
    It is two to three times faster to explicitly multiply a number variable compared to using Math.pow. That benefit even extends a little beyond integer exponents because Math.sqrt(i) is about twice as fast as Math.pow(i,.5).
    var i:int;
    var n:int = 10000000;
    var xx:int;
    var startTime:int;
    // exp .5: 2020, exp 2: 1533, exp 3: 1617, exp 4: 1427, exp 5: 1381, exp 10: 1391
    startTime = getTimer();
    for(i=0;i<n;i++){
           xx = Math.pow(i,.5);
    trace("Duration Math.pow",getTimer()-startTime);
    // exp .5: 1064, exp 2: 427, exp 3: 778, exp 4: 557, exp 5: 501, exp 10: 586
    startTime = getTimer();
    for(i=0;i<n;i++){
           xx = Math.sqrt(i);
    trace("Duration iteration",getTimer()-startTime);

  • Bit Shifting for Flags ( Constructive Criticism Please)

    I came up with a class that handles flag manipulation using Bitwise operators. Does anyone think this is more efficient than using boolean variables for flags? Or is this just overkill.
    The reason Im asking is Ive used this approach before on Muds that I have written/updated. Just wondering if it is outdated.
    * Allows for 64 different on/off flags.
    public class State
            public static final byte ST_00 = (byte)0 ;
            public static final byte ST_01 = (byte)1 ;
            ....fill here......
            public static final byte ST_63 = (byte)63 ;
         private int lowStates = 0;
         private int highStates = 0 ;
         public boolean is( byte bitIndex )
              if( bitIndex > 31 )     
                   return ( (highStates & ( 1 << (bitIndex%32) )) == 1 ? true :  false ) ;
              else
                   return ( (lowStates & ( 1 << bitIndex ) ) == 1 ? true : false ) ;
         public void remove( byte bitIndex )
              if( bitIndex > 31 )
                   highStates &= ~(1 << (bitIndex%32) ) ;
              else
                   lowStates &= ~(1 << bitIndex ) ;
         public void set( byte bitIndex )
              if( bitIndex > 31 )
                   highStates |= (1 << (bitIndex%32) ) ;
              else
                   lowStates |= (1 << bitIndex ) ;
         public void toggle( byte bitIndex )
              if( bitIndex > 31 )
                   highStates ^= (1 << (bitIndex%32) ) ;
              else
                   lowStates ^= (1 << bitIndex) ;
    * Simple implementation
    public class INeedFlags
           public static final byte FLAG_1 = ST_00 ;
           public static final byte FLAG_2 = ST_01 ;
           public State flags = new State() ;
           public static void main(String[] args)
                INeedFlags obj = new INeedFlags() ;
                obj.flags.set( FLAG_1 );
                obj.flags.toggle( FLAG_2 ) ;
                if( obj.flags.is( FLAG_1 ) )
                   System.out.println("Yep its set");
    }

    i often use bit flags.
    my variation is to have the flag constants as long bitmasks.
    this allows for multiple flag set / test in one instruction :
    if (flags.has(FLAG_1 | FLAG_3)) {..}
    also no bitshifting required should make it as fast as comparing booleans.
    ( in c++, the compiler optimises it away to almost nothing).
    the java code is a quick hack of your post (sorry about the indents)
    Adrian
      * Allows for 64 different on/off flags.
    public class State
        public static final long ST_00 = 0x00000001 ;
        public static final long ST_01 = 0x00000002 ;
        public static final long ST_02 = 0x00000004 ;
      ....fill here......
        public static final long ST_62 = 0x40000000 ;
        public static final long ST_63 = 0x80000000 ;
        public State(long bitFlags)
            state = bitFlags;
        private int state = 0;
        public long value()     
            return ( state );
        public boolean equals( long bitFlags )
            return ( state == bitFlags ) ;
        public boolean is( long bitFlag )
            return ( has(bitFlag) ) ;
        public boolean has( long bitFlags )
           return ( (state & bitFlags) != 0 ) ;
         public void clear( )
              state = 0 ;
         public void remove( long bitFlags )
              state &= ~bitFlags ;
         public void set( long bitFlags )
              state |= bitFlags ;
         public void toggle( long bitFlags )
              state ^= bitFlags ;
      * Simple implementation
    public class INeedFlags
            public static final long FLAG_1 = ST_00 ;
            public static final long FLAG_2 = ST_01 ;
            public static final long FLAG_3 = ST_02 ;
            public State flags = new State() ;
            public static void main(String[] args)
             * This still works
                 INeedFlags obj = new INeedFlags() ;
                 obj.flags.set( FLAG_1 );
                 obj.flags.toggle( FLAG_2 ) ;
                 if( obj.flags.is( FLAG_1 ) )
                    System.out.println("Yep its set");
               * but you can also say
                 obj.flags.clear( );
                 obj.flags.set(FLAG_1 | FLAG_2);
                 if( obj.flags.has( FLAG_2 | FLAG_3 ) )
                    System.out.println("one of them is set");

  • Bit Shifting

    Hello;
    I am writing some code for a piece of hardware.
    In C I would write:
    slcmd[0] = (PORT_SYS << 5 ) + CMD_PORT_SM;
    Where Port_SYS = 7 and
    CMD_PORT_SM = 0x1F
    both the PORT_SYS and CMD_PORT_SM are char variables.
    The first three bits of slcmd[0] represent a port and the last 5 represent the length of data going to the port. Or in this case 31 for a system call. When I try to compile this using JBuilder 6 I get Error # 355 possible loss of precision: int, required: char at line ....
    How can I do this in Java. I have done quite a bit of research and have not found any answers, any help would be appreciated.
    Thanks
    Don Lindsay
    LQ Computer Consultants

    The simple data type for 8 bits in Java is byte. char is 16 bits. int is 32. All data types are signed. All arithmetic's is performed on integers (i.e. byteA + byteB gives an int as the result).
    The easiest way is to deal with integers all the way. (And convert the result to byte in the end if that's what you need.)
       public static void main(String[] args)
          int ps = 7;
          int cps = 0x1f;
          int r = (ps << 5) + cps;
          System.out.println("result:"+Integer.toHexString(r));
       }The output is ff witch seems to be correct...

  • Can you optimize this code with bit shifting?

    Hi there,
    I was wondering if anyone could make this code run faster, by implementing bitshifting techniques. I'd really appreciate it, and thanks!
        public void scaleImage(Image source, Image dest) { 
            int sourceWidth = source.getWidth();
            int sourceHeight = source.getHeight();
            int thumbWidth = dest.getWidth();
            int thumbHeight = dest.getHeight();
            Graphics g = dest.getGraphics();
            int dx, dy = 0;
            for (int y = 0; y < thumbHeight; y++) {
                for (int x = 0; x < thumbWidth; x++) {
                    g.setClip(x, y, 1, 1);
                    dx = x * sourceWidth / thumbWidth;
                    dy = y * sourceHeight / thumbHeight;
                    g.drawImage(source, x - dx, y - dy, Graphics.LEFT | Graphics.TOP);
        }Message was edited by:
    conlanrios

    your method reminds me this stuff:
    http://www.java-tips.org/java-me-tips/midp/displaying-images-as-thumbnails-on-j2me-devices.html
    you can have a look to this article:
    http://developers.sun.com/techtopics/mobility/reference/techart/design_guidelines/image_resizing.html

  • Left-shift operator used on byte values

    Hello,
    I'm reviewing some problems, and I need some help. I have a program that has some code like the following:
    byte y = 10; // 00001010 in binary
    byte result = (byte) (y << 1);
    System.out.println("result: " + result); // 20.  Ok.
    result = (byte) (y << 7);
    System.out.println("result: " + result); // 0.  Ok.
    result = (byte) (y << 8);
    System.out.println("result: " + result); // 0. Why???
    // I was expecting a shift of 0 bits because the
    // right-hand operand is equal to the number of
    // bits for the size of the result type--in this case
    // 8 bits for a byte.
    // 8 % 8 = 0 number of bits for the shift.
    result = (byte) (y << 6);
    System.out.println("result: " + result); // -128.  Ok.
    result = (byte) (y << 10);
    System.out.println("result: " + result); // 0.  Why???
    // Shouldn't it be 2 bits for the shift?
    // That is, 10 % 8 = 2.
    // I was expecting 40 as the the answer for this one.I understand that for binary operations that the operands will be promoted to at least int types before execution occurs, but I still don't see how it would make a difference for the left-shift operator. Any help and clarification on this will be appreciated. It would be helpful to see the binary representation of the the "result" variable for the ones that I'm asking about. Thanks in advance.

    result = (byte) (y << 8);
    System.out.println("result: " + result); // 0. Why???
    // I was expecting a shift of 0 bits because the
    // right-hand operand is equal to the number of
    // bits for the size of the result type--in this case
    // 8 bits for a byte.the result of (y << 8) is an int, not a byte. the byte "y" is promoted to int for the bit shift. so the int result of the bit shift is 00000000 00000000 00001010 00000000. when you cast that back to byte, the 24 leftmost bits get lopped off, and you're left with zero.
    hth,
    p

  • What is the use of shifting?

    Hi,
    I am studying to become a Sun Certified Java Programmer, and I came upon bit shifting. I come from a database Oracle background and although I understand what bit shifting does, I cannot understand the need for it. Can somebody give me examples of its usefullness?
    Thanx!
    Hanne.

    Shifting can be a completely useless or indispencible operation depending on the application one is building.
    Shifting is used extensively in encyption algorithms and for converting between big-endian , and little endian byte ordering
    (Intel (IA-32) Machines like the Pentium are little Endian while Motorola for the Mac are Big endian)
    like in this code for 4 bytes of data:
    int changeEndianess(String data)
    int num = Integer.parseInt(data,16);
    System.out.println(Integer.toHexString(num));
    int ret = (((num & 0xff000000) >>> 24) |
    ((num & 0x00ff0000) >>> 8) |
    ((num & 0x0000ff00) << 8) |
    ((num & 0x000000ff) <<24) );
    System.out.println(Integer.toHexString(ret));
    return ret;
    These are only a feww of its applicaions

  • Bitwise shift in ABAP?

    Hi all,
    How can I shif data bitwise in SAP? Is it possible?
    Best Regards
    Marcin Cholewczuk

    Hi,
    Perhaps the below link could give ideas:
    http://stackoverflow.com/questions/8349886/bit-shifts-with-abap
    PS: I am not familiar with ABAP coding, as mainly into Basis - just thought could help
    Regards,
    Srikishan

  • Decoding bit values, (e.g. bit# 7 (MSB, 0=LSB) in 0xFF = 1)

    I needed to "decode" or read out the bit-status of any specified bit in any given number (from U8 to U32), and as such I made a "tiny" VI to give that functionality.
    Since I'm rahter fresh to this whole game, I would like to get some feedback on the code.
    The VI will be used as a sub-vi in a larger project, to decode status bytes which I can then use to control indicators for various processes.
    I've inlcuded just a little bit of documentation, but I have not yet made an icon as my preffered icon editor is on a different computer.
    I suspect I'm not as efficient in this vi as I could have been, but I'd like some feedback whether or not I have made any big boo-boo's, say on a scale from 1 to 10 with 10 beeing perfect.
    One comment I can give myself is that the U8 for bit-position is converted when passed to the for-loop... but on the other hand, it feels "wrong" to use the default data-type here, since the maximum value that makes sense to pass to the vi is 31 (for an U32 data-type).
    Project Engineer
    LabVIEW 2009
    Run LabVIEW on WinXP and Vista system.
    Used LabVIEW since May 2005
    Certifications: CLD and CPI certified
    Currently employed.
    Attachments:
    bitdecode.vi ‏20 KB

    If you don't want to go through the expense of allocating a 32 byte boolean array, you can just use a plain masking operation. Remember that the logical operations perform bitwise on integers. (scale by power of two is very cheap for integers, because it uses a simple bit shift, same as in example 2).
    Message Edited by altenbach on 12-04-2005 04:03 PM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    bitdecodeMOD.vi ‏14 KB
    bitdecodeMOD2.vi ‏14 KB

Maybe you are looking for