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

Similar Messages

  • 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

  • Arithmetic operations using Picklist field values

    Hi,
    I tried to multiply a picklist field value as below and wanted to store the result in a field (datatype = Integer or Number) using a workflow which triggers based on the Trigger Event "Before modified record saved".
    Criticality = [<plFrequency_of_Occurrence_ITAG>] * 10
    It throws an error:
    "Update Criticality : Value too long for field 'ZNum_0' (maximum size 16). (SBL-DAT-00235)(SBL-ODS-00500)"
    Can't we do arithmetic operations on the Picklist fields? I believe this is because the values are being stored as text??
    Do we have a function to convert the character field to numeric before we perform any arithmetic operations?
    Thanks,
    Sriram

    Hi,
    You're right, you can't perform arithmetic operations with a number and a text. That's why you get the error.
    Today, there's no function you can use to convert a text to a number.
    You'll need to create a SR asking for a Change Request.
    Regards,
    Max

  • Left shift key is a problem

    This is what I get on a page when the bottom row of keys are pressed in sequence from left to right:
    No shift keys used:         zxcvbnm,./
    RIGHT SHIFT KEY USED:  ZXCVBNM<>?
    LEFT SHIFT KEY USED:   BN
    In other words, when I use the left shift key, the letters Z, X, C, V, M, <, >, and ? don't appear at all. What is causing this?

    I have two last things to try. The first is a hard reset. The instructions for that are here:  Performing a Hard Reset or Forced Reset
    If that doesn't help, the last option is to try to restore your BIOS. The instructions are here: Restoring the BIOS
    If that fails, it will most likely be a hardware issue. I am not certain of your warranty coverage, you can find that information from support, as well as your repair options.
    Alternatively, you might be able to look your warranty status up here: Check your product warranty
    HP technical support: 800-474-6836. If you live outside the US/Canada Region please click the link HERE to get the support number for your region.
    TwoPointOh
    I work on behalf of HP
    Please click “Accept as Solution ” if you feel my post solved your issue, it will help others find the solution.
    Click the “Kudos, Thumbs Up" on the bottom to say “Thanks” for helping!

  • 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.

  • I cannot type a capital i using the left shift key. also keyboard now putting the cursor in a different place than where i type. Checked speech keys, all fine. Batteries fine.

    i cannot type a capital i using the left shift key. also keyboard now putting the cursor in a different place than where i type while i am typing. Checked speech keys, all fine. Batteries fine. Also used the keyboard check program on the imac, shows key ok.
    wireless mouse will suddenly scroll down horizontally and won't stop. when i am on a webpage it will bounce back to the previous page without my doing so. i have tried adjusting the mouse several times, turning it on and off, and now am using my wacom mouse which also seems to have issues with this computer.  i have only had this a few months and getting very frustrated.

    Can you try a different keyboard to see if the problem persists? Also, you have 90 days of phone support when you buy a mew mac.

  • 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

  • Changing of byte value

    byte value is -128 to 127
    i want to make it 0 to 255
    the return result must be in byte form
    is it possible?
    how?
    can bit mapping work?
    tq

    fmpfmpf wrote:
    because i want to try to do it in byteAs long as the value is always a byte, it doesn't matter whether the JVM thinks it's 255 or -1. The problem is, every time you perform any kind of operation on a {color:000080}byte{color} or a {color:000080}short{color}, the JVM promotes it to an {color:000080}int{color}, preserving the sign in the process. Then you have to mask it to recover the original value. Save yourself that hassle and keep using {color:000080}int{color}s.

  • Doing XOR with many byte values - am I stupid?

    Hi,
    I am having a problem by using the Java XOR Operator.
    Sonething, call it module, delivers me an array of bytes. It includes different byte values and in the last field the checksum. The checksum you can get by doing:
    (((1st Databyte XOR 2nd Databyte) XOR 3rd Databyte) XOR 4th Databyte) XOR ....
    and so on. So I wrote the piece of code:
    byte checksum = ((Integer)dataVector.get(0)).byteValue();
    for(int i = 1; i < dataVector.size()-1; i++){
      checksum= (byte)(checksum^ ((Integer)dataVector.get(i)).byteValue());
    if(checksum == ((Integer)dataVector.get(dataVector.size()-1)).byteValue())
      System.out.println("Yeah! Got it!");OK, it's a Vector, i see, but anyway I am handling with byte values so that doesn't matter.
    So please say there is a mistake in the code.

    I would implement it a little differently but your approach is correct too. Examine this small program:public class Checksum {
        // my way
        static boolean check(byte[] b) {
         byte checksum = 0;
         for (int i = 0; i < b.length; i++) {
             checksum ^= b;
         return checksum == 0;
    // your way
    static boolean check2(byte[] b) {
         byte checksum = b[0];
         for (int i = 1; i < b.length - 1; i++) {
         checksum ^= b[i];
         return checksum == b[b.length - 1];
    public static void main(String[] args) {
         // 98 is all the other numbers XOR'ed together:
         byte[] array = {34, 124, -45, 76, 9, 71, -34, -56, -5, 98};
         System.out.println("Check1: " + check(array));
         System.out.println("Check2: " + check2(array));
    // introduce a small mistake in the first element:
    byte[] wrongArray = {24, 124, -45, 76, 9, 71, -34, -56, -5, 98};
    System.out.println("Check1: " + check(wrongArray));
    System.out.println("Check2: " + check2(wrongArray));
    }The output is what you'd expect: Check1: true
    Check2: true
    Check1: false
    Check2: falseEither your checksum is not calculated the way you think it is or there's some other problem. Why do have a Vector of [i]Integers when you should be working with bytes?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • KEY LOOKUP Operator using TCL

    Hi,
    I am trying to create a key lookup operator using tcl.
    OMBALTER MAPPING 'TESTMAP' ADD KEY_LOOKUP OPERATOR 'INSTITUTION_CHARACTERISTIC_DIM'\
    SET PROPERTIES (BOUND_NAME,CREATE_NO_MATCH_ROW, LOOKUP_CONDITION)VALUES
    ('INSTITUTION_CHARACTERISTIC_DIM','true','OUTGRP1.INSTITUTION_CHAR_SRCKEY
    = INGRP1.INST_CHAR_SRCKEY')
    and then adding an attribute to the INGRP
    OMBALTER MAPPING 'TESTMAP'ADD ATTRIBUTE 'LAST_ACTIVITY_DATE' OF GROUP 'INGRP1' OF OPERATOR 'INSTITUTION_CHARACTERISTIC_DIM'
    This is creating a key lookup operator by the name
    INSTITUTION_CHARACTERISTIC_DIM, and i can see the Bound Name in the
    properties window, but when i double click and open the key lookup, i
    dont see the result table under the LOOK UP tab.Because of this, the
    attributes dont get added in the OUTGRP of the key lookup.
    Am i missing some important property of a key look up?
    can some one please help me with this...
    Thanks in advance...

    Hi,
    you have to bind the lookup operator to the table:
    OMBALTER MAPPING 'MAP_LOOKUP' ADD KEY_LOOKUP OPERATOR 'MY_LOOKUP' BOUND TO TABLE '/MY_PROJECT/MY_DB/DIM_CUSTOMERS'
    OMBALTER MAPPING 'MAP_LOOKUP' MODIFY OPERATOR 'MY_LOOKUP' SET PROPERTIES (CREATE_NO_MATCH_ROW, LOOKUP_CONDITION)VALUES ('true','OUTGRP1.ID_CUSTOMER=INGRP1.ID_CUSTOMER')
    OMBALTER MAPPING 'MAP_LOOKUP' ADD ATTRIBUTE 'ID_CUSTOMER' OF GROUP 'INGRP1' OF OPERATOR 'MY_LOOKUP'Regards,
    Carsten.

  • Using LDAP to search attribute bit flags using attribute OID values

    Hello everyone,
    My question stems from trying to understand the OID and syntax behind this classic LDAP search to find disabled users:
    "(useraccountcontrol:1.2.840.113556.1.4.803:=2)"
    What I am interested in is the value 1.2.840.113556.1.4.803, specifically how it differentiates from the value 1.2.840.113556.1.4.8, which is the OID of the useraccountcontrol attribute:
    http://msdn.microsoft.com/en-us/library/ms680832(v=vs.85).aspx
    Now, this website below says that the 03 and 04 are designators of the AND and OR operations, respectively, and are added on to the end of the OID:
    https://www.appliedtrust.com/blog/2011/04/keeping-your-active-directory-pantry-order
    However, using this logic, I can't get these 03 and 04 operators to work with other attribute OID's that use flags as values, such as the "searchflags" attribute, e.g. a LDAP search of "(searchflags:=1.2.840.113556.1.2.33404:=0)
    returns nothing, using the OR (04) operation at the end of the "searchflags" OID of 1.2.840.113556.1.2.334.
    So back to my original question, for the useraccountcontrol OID of 1.2.840.113556.1.4.8, is this OID at all related to the bitwise AND extensible match of 1.2.840.113556.1.4.803 (like just adding a 03 to designate an AND operation), or is this
    extensible match
    value of 1.2.840.113556.1.4.803 completely separate from the useraccountcontrol OID of 1.2.840.113556.1.4.8?
    If I have my terms mixed up, please feel free to correct me on what the proper terms are.
    Thanks!

    Hmm yeah I posted that link above in my OP as well, and I was hoping that the OID values of these bitwise filters were somehow related to the shorter OID of the "useraccountcontrol" attribute, but it looks like it's just a coincidence.
    So I wonder if the "useraccountcontrol" section of
    this article from my OP is a little misleading when it says:
    To make a comparison, we either need to use the LDAP_MATCHING_RULE_BIT_AND rule (1.2.840.113556.1.4.803), or the LDAP_MATCHING_RULE_BIT_OR rule (1.2.840.113556.1.4.804) for our attribute OID (the AND rule adds a 03 suffix to denote the AND operation,
    and the OR rule adds a 04 suffix).
    Following this logic, I should be able to use the "03" and "04" in other bitwise operations with different OID's to search "AND" or "OR", but as I pointed out in my OP above, I can't seem to make this work with adding the 
    "03" and "04" onto the end of other OID's. So I will go with Christoffer that these bitwise OID's (1.2.840.113556.1.4.803 and 1.2.840.113556.1.4.804) are unique in themselves, and the fact that they are 2 characters away from the OID of the "useraccountcontrol"
    attribute (1.2.840.113556.1.4.8) is just coincidence.
    This does seem strange however, and it seems like there should be some correlation here....
    If anyone has any more info, I would love to hear it!

  • How do I use the Index Values property node with a multidimensional array.

    I am using a 2D array to store operator inputs on my front panel.  I only want to display one element to the operator at a time.  I am trying to use the Index Values property node to change the displayed element.  However, I can only get the Rows index to work.  How do I direct the Columns index as well?  The help says to use one per dimension.  I need clarification on what this is talking about.  I've tried adding a second element to the property node, 2 seperate property nodes, and diferent wiring techniques. (series, parallel)

    If you only wire up one of the inputs (col or row) what you get out is a 1D array of either the column or row. If you wire controls to both, then you will get one element out of the array. Getting a single element in a 2D array requires you to specify both a row and column.
    Message Edited by Dennis Knutson on 02-08-2007 08:34 AM
    Attachments:
    Index 2D Array.PNG ‏2 KB

  • Using a variable value

    Hi there!
    Please, first, promise not to laugh!
    In the script, I wrote:
    text_field.text = "yes";
    var a:String = text_field.text;
    At the beginning of a conditional logic statement I want to
    use the value of this variable.
    How should I write the condition?
    I tried:
    if (a == yes) {....
    if (a = yes) {....
    if (a == "yes") {....
    if (a.length = 3) {....
    Nothing works...
    Maybe I'm too tired and... confused!
    Please, put me back on the track.
    I thank you in advance.
    Best regards from Old Gerry

    GerryIsHere,
    > In the script, I wrote:
    > text_field.text = "yes";
    > var a:String = text_field.text;
    I'm with you so far.
    > At the beginning of a conditional logic statement I want
    > to use the value of this variable.
    > How should I write the condition?
    The a variable is typed as a string, so its value must be a
    string.
    > I tried:
    > if (a == yes) {....
    Because "yes" does not appear in quotes, Flash will
    interpret it as a
    variable. Since you haven't declared a yes variable anywhere,
    it's value
    will be undefined or null, so your first condition compares a
    with, say,
    undefined, which evaluates to false.
    > if (a = yes) {....
    This isn't a comparison at all, actually. The single equals
    sign is an
    active operator that *sets* the value of one thing to
    another. Here, you're
    setting the value of a to, again, undefined (or perhaps
    null).
    > if (a == "yes") {....
    This should do it. When you say nothing works, how are you
    determining
    that?
    > if (a.length = 3) {....
    Again, the single equals sign is killing it. You're trying
    to set the
    String.length property to a number, but the length property
    is read-only.
    Is it possible that text_field isn't a correct reference to
    one of your
    text fields? One of your text fields would have to have the
    instance name
    "text_field" (minus quotes) in order to be referenceable by
    ActionScript.
    David Stiller
    Co-author, Foundation Flash CS3 for Designers
    http://tinyurl.com/2k29mj
    "Luck is the residue of good design."

  • 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.

Maybe you are looking for

  • FEEDBACK BUTTON MISSING !! do you really not care about our oppinion?

    dear sirs, for the number 1 software ,it is still totally incromprehensive to me,how you have not installed an easy FEEDBACK button in your softwares,especially in adobe's photography directory, which seems to be in a continous coma ,since you starte

  • Blackberry World Not Functioning?????

    For the past couple of days, I have been unable to purchase apps. The page pops up that "the payment server/system is busy.....try the transaction later." This, of course, is not a problem. However, it has become very frustrating since it has gone on

  • Text is missing in documents created through F110& F111

    Hi Friends, We are facing an issue . Text is getting blanked in documents created via transactions F110 & F111. Please help. Thanks & Regards Bhairavi

  • OS 10.8.3 saves unwanted changes

    Just upgraded OS to 10.8.3 from 10.6.?.  Have a couple of quirks I'm hoping someone can resolve. 1st one is, when I make any changes to a document in pages or a spreadsheet in numbers, I don't always want to save the changes.  In the past I would sim

  • Customer pick up the goods from plant

    In SNC (5.1)  we get the delivery date for firm receipts on web UI . This is based upon the transplantation lane which has transit time. For eg 4days i.e. 96 hrs. Now the issue is sometimes customer pick up the material directly from plant.  How this