shift

Bitshifting in C++ producing the wrong answer

扶醉桌前 提交于 2019-12-05 09:31:48
I tried running the following code code: char c = (2 << 7) >> 7 which should return 0 because 2 has this binary representation as a char : 0 0 0 0 0 0 1 0 After 7 shifts left, we get 0 0 0 0 0 0 0 0 Then, after seven shifts right, we get 0 0 0 0 0 0 0 0 However, I'm getting the result as 2, not 0. The compiler says that 2 << 7 is 256, but it's a char and so it shouldn't be 256. I understand that the 2 << 7 will be calculated as int s and the answer will be put into c so 256 >> 7 is 2. I tried to cast 2 to char (ex: (char)2>>7 ) but it doesn't work either. I'm trying to extract each bit from

Why does right shifting -1 always gives -1 in PHP?

萝らか妹 提交于 2019-12-05 07:46:08
I am trying to figure out why if I shift the negative integer -1 I always get -1, e.g.: echo -1 >> 64; // -1 echo -1 >> 5; // -1 echo -1 >> 43; // -1 echo -1 >> 1; // -1 Whatever second operand of the right shift is given, -1 remains -1... I do understand that when you perform a right shift you're actually doing this: x >> y = x / 2^y But in the case of x being -1, if so, I do: -1 >> 3 = -1 / 2^3 Shouldn't this value be -1/8 = -0.125? Thanks for the attention. Bitwise shift operators don't divide. They do what they are supposed to do - shift bits. In particular, the right shift operator does

Rotate left verilog case

荒凉一梦 提交于 2019-12-04 22:10:15
My task is to write a 16 bit ALU in verilog. I found difficulties when I do the part that needs to rotate the operand and doing the 2's complement addition and subtraction. I know how to work that out by paper and pencil but i cant figure out ways to do it in Verilog. for example: A is denoted as a15 a14 a13 a12 a11 a10 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 if i am going to rotate 4 bits, the answer would be a11 a10 a9 a8 a7 a6 a5 a4 a3 a2 a1 a0 a15 a14 a13 a12 i tried concatenation but it turns out to be incorrect. need you all help... Why is concatenation incorrect? This should do what you ask.

how to do circular shift in numpy

雨燕双飞 提交于 2019-12-03 23:26:05
I have a numpy array, for example a = np.arange(10) how can I move the first n elements to the end of the array? I found this roll function but it seems like it only does the opposite, which shifts the last n elements to the beginning. Why not just roll with a negative number? >>> import numpy as np >>> a = np.arange(10) >>> np.roll(a,2) array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7]) >>> np.roll(a,-2) array([2, 3, 4, 5, 6, 7, 8, 9, 0, 1]) you can use negative shift a = np.arange(10) print(np.roll(a, 3)) print(np.roll(a, -3)) returns [7, 8, 9, 0, 1, 2, 3, 4, 5, 6] [3, 4, 5, 6, 7, 8, 9, 0, 1, 2] 来源:

Shuffle a List in Scala [duplicate]

不想你离开。 提交于 2019-12-03 22:49:32
This question already has an answer here : Closed 3 years ago . Scala ListBuffer (or equivalent) shuffle (1 answer) I have question to for shuffle list in scala using scala.util.Random . For example I have val a = cyan val b = magenta val c = yellow val d = key val color = Random.shuffle.List(a,b,c,d).toString //but it doesn't work ;( so I want the val color to be random order of val a, b, c and d . User Scala's Random class method shuffle: scala.util.Random.shuffle(List(a,b,c,d)) 来源: https://stackoverflow.com/questions/39990858/shuffle-a-list-in-scala

Shell shift procedure - What is this?

戏子无情 提交于 2019-12-03 01:34:58
In shell we have the command shift, but i saw on some example its giving shift 3 Why there is a number after shift ? and what its about ? what it does ? Example: echo “arg1= $1 arg2=$2 arg3=$3” shift echo “arg1= $1 arg2=$2 arg3=$3” shift echo “arg1= $1 arg2=$2 arg3=$3” shift echo “arg1= $1 arg2=$2 arg3=$3” shift The output will be: arg1= 1 arg2=2 arg3=3 arg1= 2 arg2=3 arg3= arg1= 3 arg2= arg3= arg1= arg2= arg3= But when i add that, it doesn't display it correctly. Take a look at the man page, which says: shift [n] The positional parameters from n+1 ... are renamed to $1 .... If n is not given,

Can anybody please explain (my $self = shift) in Perl

淺唱寂寞╮ 提交于 2019-12-02 17:40:40
I'm having a really hard time understanding the intersection of OO Perl and my $self = shift; The documentation on these individual elements is great, but none of them that I've found touch on how they work together. I've been using Moose to make modules with attributes, and of course, it's useful to reference a module's attribute within said module. I've been told over and over again to use my $self = shift; within a subroutine to assign the module's attributes to that variable. This makes sense and works, but when I'm also passing arguments to the subroutine, this process clearly takes the

How i can shift one row of data frame to first row?

纵然是瞬间 提交于 2019-12-02 17:30:41
问题 How i can shift one raw of data frame to first raw, i want the id raw be the first raw. in R. Sepal.Length Sepal.Width Petal.Length Petal.Width Species 5.1 3.5 1.4 0.2 setosa 4.9 3.0 1.4 0.2 setosa 4.7 3.2 1.3 0.2 setosa 4.6 3.1 1.5 0.2 setosa 5.0 3.6 1.4 0.2 setosa id A B C D 回答1: We can use grepl to create a logical vector based on the 'id' in 'Sepal.Length', then set the column names of the dataset by extracting that row while removing the row from the original dataset i1 <- grepl("id",

How i can shift one row of data frame to first row?

孤人 提交于 2019-12-02 07:41:33
How i can shift one raw of data frame to first raw, i want the id raw be the first raw. in R. Sepal.Length Sepal.Width Petal.Length Petal.Width Species 5.1 3.5 1.4 0.2 setosa 4.9 3.0 1.4 0.2 setosa 4.7 3.2 1.3 0.2 setosa 4.6 3.1 1.5 0.2 setosa 5.0 3.6 1.4 0.2 setosa id A B C D We can use grepl to create a logical vector based on the 'id' in 'Sepal.Length', then set the column names of the dataset by extracting that row while removing the row from the original dataset i1 <- grepl("id", df1$Sepal.Length) setNames(df1[!i1,], unlist(df1[i1,])) # id A B C D #1 5.1 3.5 1.4 0.2 setosa #2 4.9 3.0 1.4

Allign the words to the specified column in vim using commands

微笑、不失礼 提交于 2019-12-02 03:37:10
问题 How I can move or shift the words in the entire file to the specified column? For example like below: Before : 123 ABC 112 XYZS 15925 asdf 1111 25asd 1 qwer After : 123 ABC 112 XYZS 15925 asdf 1111 25asd 1 qwer How it can be done using command mode? Here the thing is we need to shift the 2nd word to the specified column Here the specified column is 8 回答1: Approach with built-in commands First :substitute the whitespace with a Tab character, and then :retab to a tab stop to column 8, expanding