Left Shift Operator

class LeftShift
public static void main(String aa[])
  int a = 1;
  for(int j=0; j<67; j++)
   int k = a << j;
   System.out.println("Shifting by " + j + " : "  + k);
}This code gives the following outout
Shifting by 0 : 1
Shifting by 1 : 2
Shifting by 2 : 4
Shifting by 3 : 8
Shifting by 4 : 16
Shifting by 5 : 32
Shifting by 6 : 64
Shifting by 7 : 128
Shifting by 8 : 256
Shifting by 9 : 512
Shifting by 10 : 1024
Shifting by 11 : 2048
Shifting by 12 : 4096
Shifting by 13 : 8192
Shifting by 14 : 16384
Shifting by 15 : 32768
Shifting by 16 : 65536
Shifting by 17 : 131072
Shifting by 18 : 262144
Shifting by 19 : 524288
Shifting by 20 : 1048576
Shifting by 21 : 2097152
Shifting by 22 : 4194304
Shifting by 23 : 8388608
Shifting by 24 : 16777216
Shifting by 25 : 33554432
Shifting by 26 : 67108864
Shifting by 27 : 134217728
Shifting by 28 : 268435456
Shifting by 29 : 536870912
Shifting by 30 : 1073741824
Shifting by 31 : -2147483648
Shifting by 32 : 1
Shifting by 33 : 2
Shifting by 34 : 4
Shifting by 35 : 8
Shifting by 36 : 16
Shifting by 37 : 32
Shifting by 38 : 64
Shifting by 39 : 128
Shifting by 40 : 256
Shifting by 41 : 512
Shifting by 42 : 1024
Shifting by 43 : 2048
Shifting by 44 : 4096
Shifting by 45 : 8192
Shifting by 46 : 16384
Shifting by 47 : 32768
Shifting by 48 : 65536
Shifting by 49 : 131072
Shifting by 50 : 262144
Shifting by 51 : 524288
Shifting by 52 : 1048576
Shifting by 53 : 2097152
Shifting by 54 : 4194304
Shifting by 55 : 8388608
Shifting by 56 : 16777216
Shifting by 57 : 33554432
Shifting by 58 : 67108864
Shifting by 59 : 134217728
Shifting by 60 : 268435456
Shifting by 61 : 536870912
Shifting by 62 : 1073741824
Shifting by 63 : -2147483648
Shifting by 64 : 1
Shifting by 65 : 2
Shifting by 66 : 4
Things are fine till shifting by 31, but I don't understand why the entire pattern repeats after that.

Well, int has 32 bits, and when you start left shifting 1, which has one at first position and zero at all other, you keep shifting this one to left, and keep inserting zero from the right side. But when you shift it by 32, the only available one should gets off the available 32 positions and I expect to have zero at all the positions, that should give me output as 0. It, however, is giving 2!
a = 1 00000000 00000000 00000000 00000001
a<<1 00000000 00000000 00000000 00000010
a<<2 00000000 00000000 00000000 00000100
a<<31 10000000 00000000 00000000 00000000
a<<32 00000000 00000000 00000000 00000000and therefore, I expect to get 0

Similar Messages

  • 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

  • Logical right shift ( ) operator

    Hi,
    I suspect a bug in the logical right shift operator >>> (as opposed to the arithmetic right shift >>).
    The documentation says "0-s will be shifted from the left". It is not the case.
    byte b = -128;                         // this means 0x80 in hex notation
    byte b1 = (byte)(b >>> 4);I would expect b1 = 0x8 (0-s shifted in from the left) but the result is
    b1 = 0xF8 instead, as if I had used the >> operator. The sign bit was shifted in from the left instead of 0-s.
    Of course, there is a workaround:
    byte b1 = (byte)((b >>> 4) & 0x0F); which is the same operation as using >>
    If this is the case, what is the purpose of using >>> instead of >> ?
    Anyway, the ONLY thing that frustrates me in the Java language that it does not support unsigned integers. It would be important for embedded systems where unsigned numbers are used more frequently.
    Any suggestions?
    Thank you,
    Frank

    sabre150:
    It depends on the size of processor you use. The Java program runs on a 32- or 64-bit processor, so there is no problem promoting from byte to short or int. But I am talking to an 8-bit processor over a network. So I get a byte stream that has to be converted to numbers here on the Java side. The data types are various: byte or short, even sometimes two 4-bit nibbles (here comes the >>> operator), signed or unsigned. If I get a two-byte sequence, I have to convert it to a short, signed or unsigned. That means in Java promoting the byte to int, shift left the high byte, OR the low byte then cast it back to short if signed, or mask with 0xFFFF if unsigned. The resulting int can be converted to string in the usual way. With distinguished signed and unsigned types, this would be done by the compiler. But I come from the Assembler (and then Pascal/Delphi) world, so it is not a big problem for me.
    I agree that this feature is used very rarely, I already wrote the conversion routines, so why bother? As a newcomer to Java (after struggling with C for years, I love it), I just wanted to see whether other people have a better and more efficient idea.
    Thank you all for your replies!
    Frank

  • My left shift key on Macbook Pro Retina (15") is behaving like the F11 key (show/hide open windows).  The key behaves the same for the laptop keyboard and for an external Apple keyboard?  This just started in last week after 6 months of use

    My left shift key on Macbook Pro Retina (15") is behaving like the F11 key (show/hide open windows). 
    The left shift key behaves the same on the laptop keyboard and on an external Apple keyboard. 
    This just started in last week after 6 months of use.  The right shift key and other keys on the keyboard all appear to work correctly.
    Suggestions greatly appreciated.

    I have a solution to this issue now after a discussion with Apple Support.  Some where in the last few weeks, there was a change in the key that would do a Show Desktop under the Mission Control settings in the System Preferences.  Even though the key sequence that was shown for the Show Desktop setting did not include the Left SHift key, by changing this first to having no key sequence and applying that and then changing it back to the F11 key, the Left SHift key issue that I was having has cleared up.
    A second issue that was uncovered was that the SSD had some issues (uncovered when I ran Disk Utility using the Repair Disk key).  I had to restart and use the Disk Utility through the Command R sequence to get to the stand alone Disk Utility.  The SSD appears to be running better as a result as well.
    Hope that this is of use to someone else in the future.

  • How do I fix my left shift key? Any open windows leave and make the desktop visible.

    How do I fix my left shift key? Any open windows leave and make the desktop visible. I have tired to go into system preferences and reset all of the keyboard settings to default but the issue hasn't gone away.

    You are not running as administrator, I think, which may be required and the only way to properly work and register. Also check for compatibility mode settings, UAC and DEP, all of which may require to be adjusted for a legacy app like PS CS2 to run properly. Also turn off Aero and switch to a conventional visual theme. The brush issue may be related to that or is a more generic problem with your graphics card and mouse config, but since you didn't provide any info on that, it will be hard for anyone to advise.
    Mylenium

  • Left shift key doesn't work on some characters

    In some applications, such as Excel and AOL, my left shift key doesn't work on some letters. Consequently, when I'm typing I get lower case letters when I've typed shift plus the letter to get a cap. Right shift works OK, but I'm not used to using it. It happens on i, t, and p, I've noticed so far. I don't know if it's the keyboard or the application. Word seems to work fine.
    Would welcome any ideas on what is causing this and how to fix it.
    Powermac G5   Mac OS X (10.4.7)  

    Is it possible that you have inadvertently switched to a different keyboard layout? Open System Preferences, then click on the Language & Text preferences. Within the Language & Text preferences click on the 'Input Sources'. You should see a long list of languages representing the various keyboard layouts that are available. Make sure that only the keyboard language you use is selected. If there are multiple languages selected then de-select the extras.
    If you continue to have problems then check the 'Show input menu in menu bar' checkbox in the lower section of the preference pane. This will place an icon in the upper right menu bar (it looks like a flag representing the language you have chosen for your input device) that provides additional options. I suggest selecing the 'Show Keyboard Viewer' option and then try pressing the keys that you are having problems with to see how they show up on the keyboard viewer graphic. If you've set the correct language for your keyboard and the key sequences still don't match, then you may need to take your keyboard in for service. Better yet, borrow another keyboard to confirm that the keyboard is the issue.

  • Why does my Left Shift +Num Keys produce Euro characters and Right Shift + Num Keys produce American?

    I am running Lion (10.7.4) on my 2010 13" MBP. When I press my Left Shift and various Num Keys (1-9), I get Euro characters (⁄ € ‹ › fi fl ‡ ° · ‚). When I use my Right Shift, I get the standard American characters (! @ # $ % ^ & * ( )).
    I have already tried restarting my computer, but this has not helped. In my language/keyboard settings, everything is set to USA. I do have multiple inputs on this machine but the flag is always on US. Occasionally, I switch to Japanese keyboards but those are not active and the problem described above appears when the US flag is displayed.

    It looks like your right shift is somehow also depressing the option/alt key.  I would suspect damage to your keyboard, which may need repair/replacement.

  • T40's Left Shift key makes right click/context menu appear?

    Hello All-
    On my T40, on which I just recently got Windows 7 correctly installed, has a curious behavior:
    When I press the left shift key, it functions the same as if I had pressed the right mouse button.  It functions correctly otherwise, I.E., when using it to capitolize a letter in an email, the letter is capitolized, but as soon as I let go of the shift key, the right click/context menu immediately appears.
    This is exceedingly irritating, as I cannot continue typing until I have navigated out of the menu.
    I have checked to see if the Keyboard Customiser utility is causing this, as I currently have my left Alt key functioning as a Windows button, but it does not appear to be causing this particular issue.
    Does anyone have any idea how I might go about fixing this?
    Thanks in advance,
    G
    Solved!
    Go to Solution.

    Nevermind-
    Had the keyboard customizer set to use the left shfit key as the application key.  Duh.

  • 3 letters + left shift key not working - 2 of them + right shift key working

    Hi all,
    as in subject, I have just bought a laptop g50-45, italian keyboard.
    I encountered this problem:
    "LEFT shift key" + "e" or "d" or "c" combinations do not work. Nothing happens.
    "RIGHT shift key" + "e" or "d" work properly
    "RIGHT shift key" + "c" do not work. Nothing happens
    CAPS key + "e" or "d" or "c" work properly.
    I noticed this issue before making any updates of  windows 8.1
    I tried making an update of the bios and later updating windows but the problem is still there.
    I check with an external keyboard and everything work correctly
    I don't think it could be a mechanical problem because "e", "d" and "c" keys alone work properly, so it seems more a software side problem.
    Thank you for your help.

    thanks, i thought there some quick procedure to do before heading to apple store. i was just wondering, it was working for a while, and then suddenly like that. i have macbook 10.6.8 software. intel core 2 duo. less than a year old, if i'm not mistaken.

  • My left shift key stopped working right one is fine

    my left shift key stopped working this morning. never had a problem tell I started using snapshot shift plus command

    Keyboard Keys Do Not Respond

  • My left shift keyboard not working

    APPLE support team and all. please help me.  any solution to solve this problem ? when i click left shift now working. for right its working well.

    Maybe you should ask on the Logitech support community.

  • Right shift and c not working, Left shift and c works fine for a capital c

     I have a Lenovo G560 laptop. Had this problem once before and computer tech fixed problem, but moved cities. Worked fine but recently had a windows update. Now when I press the right shift key and c nothing happens. Left shift and c displays a capital c. How do I restore the right shift c ?

    hi richiestac,
    You can try to remap the Right Shift Key so that it will function as Left Shift using Sharpkeys.
    To do this:
    1. Download and install Sharpkeys
    2. Open Sharpkeys, click on Add and on the Map this key (From Key) , select the Right Shift key, then on the To this key (To key), select the Left Shift key.
      - Link to picture
    3. When finished, click Write to Registry and reboot.
    As for the display driver installation, you need to uninstall first any signs of Intel HD Graphics from the Control Panel (Icon View) > Programs and Features. After that, open Device Manager > Display Adapters and uninstall the Intel Graphics (ensure you put a check on Delete the driver software for this device and try to re-install the Intel Onboard Graphics Driver.
    Alternatively, you can skip all the procedures above and just press the OneKey Recovery button to restore the system to factory settings (note that all data will be wiped on the OS partition).
    Regards
    Did someone help you today? Press the star on the left to thank them with a Kudo!
    If you find a post helpful and it answers your question, please mark it as an "Accepted Solution"! This will help the rest of the Community with similar issues identify the verified solution and benefit from it.
    Follow @LenovoForums on Twitter!

  • I need a replacement key for left shift, DV4 2106tx.

    This model is new and i can't find a spare key for the left shift key , the retainer broke and i can't do anything about it right now except asking for help, please tell me where and how can i get the spare key with retainers (i am also ready to buy the whole key set if required). Thank you.

    If it is new, have you try calling HP? They may send you a new keyboard.
    or
    try ebay, such as here
    Pavilion DV2922TX, XP-SP3 32bit, Intel T5750 2.0Ghz, Nvidia Geforce 8400M GS with 128MB, 4GB 667 DDR2, 250GB HDD

  • Left Shift - w doesn't work

    I'm unable to get a capital w when I use the left shift key. I am able to capitalize every other letter with the left shift key and I can get a capital W with the right shift key and the caps lock button. Anybody have a similar experience or have any recommendations to fix this?

    Go to the System Preferences->International->Input Menu and check the box "Keyboard Viewer" option. Also check the box at the bottom which says "Show input menu in menu bar". Now, access the Keyboard Viewer from the input menu in the menu bar (it looks like a little flag representing the language you are using) so that it displays a pictorial representation of your keyboard.
    With the Keyboard Viewer open try typing the 'W' and left 'shift' key combination together and observe whether the corresponding keys on the Keyboard Viewer highlight. This can help you determine whether you have a software issue, or whether your keyboard is not working correctly. I suspect software since the shift key seems to work with all of the other keys.
    Is it possible that somebody accidentally set a 'Keyboard shortcut" in the keyboard preferences under the 'Keyboard Shortcuts' tab? If in doubt try pressing the 'Restore Defaults" button within the Keyboard Shortcuts preferences to see if that helps.

  • Left SHIFT key always truncated with OPTION key

    Every time I press down the left shift key the option key also gets activated. For instance, leftshifta=Å, leftshiftq=Œ and so forth. I noticed that when I went through the initial setup and confirmed with the keyboard viewer. How can I get this fixed without taking the computer back?

    Welcome to Apple Discussions
    You can't fix this yourself you'll have to take it back to Apple and have them replace the keyboard.

Maybe you are looking for

  • I can't take my Visa card off of my account. Why is this?

    I Can't take my Visa card off of my account. Why is this?

  • Saving a slideshow without exporting?

    Is this possible? I want to do a few different slideshows, but I can't seem to find a way to save them without exporting. This is a pain because every time I want to make a change, I have to start over and re-export. Surely I am just missing somethin

  • Internal Table and Structures

    Hi, I am a beginer. I know how to create a structure and how to create an internal table using ABAP/4. My problem is, i don't understand where to use internal table and structure, also i find myself very confused about the explicit work areas. Plese

  • HT201272 Re downloading songs.

    On my iTunes account I have songs from my old phone that are not on my new phone... In my account they have a little cloud with an arrow pointing down beside it, if I hit it and re download it, am I paying for that song again???

  • Doubt in exits for miro tcode

    Hi all,    I have to change the account posting no in MIRO transaction.For this an user exit LMR1M002 is used but this fn module doesn't hold the value for a/c details.So i checked with BADI INVOICE_UPDATE.But I am unable to find whether that BADI is