java - How to cut binary number when moving left? -


how cut binary number when moving left instead of expand number of bits?

int num = 0b101010; num = num << 1 // want num 01010  num = num << 1 // want num 1010 num = num << 1 // want num 010 num = num << 1 // want num 10 

is there easy way it?

yes, easy do: make mask ones (0b111111 in case), , use mask original number using &. shift mask right, effect below:

number mask   result ------ ------ ------ 101010 011111 001010 101010 001111 001010 101010 000111 000010 101010 000011 000010 101010 000011 000010 101010 000001 000000 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -