Implementing a logical shift is easy using the << or >> operator, but what about arithmetic right shift (sign extension) how to do it in Chisel?
In Chisel cheat sheet it is mentioned that >> operator is used for arithmetic right shift which is the same as logical right shift operator.
I think the sign will be extended if the type is SInt.
Related
I have an algorithm that performs division of 2 64-bit unsigned integers using C bitwise operators (<<, &, ^, |, ~) in a loop.
Now I would like to eliminate shift left << operator completely to understand how this is done as I can already synthesize AND, OR, XOR, NOT using NAND gates.
Is it possible to perform left shift by 1 with JUST NAND gates too? I have read a little about flip-flops in electronics but I'm implementing this in pure software just to understand it.
I want to avoid using << or >> operators completely and do not want to use existing arithmetic operators from any computer language including assembly.
In hardware, you can implement left shift by 1 without any logic gates at all. Just wire the data lines like so:
If you want something more generic, you could implement a barrel shifter. This can be synthesised from multiplexers:
(source: books24x7.com)
which in turn can be synthesised from NAND gates.
Here is a comprehensive master's thesis on the subject: Barrel Shifter
Design,
Optimization, and
Analysis.
For bit-wise shift (or rotation, circulation) operations, we usually have an operator, i mean two of them, for instance
x << n
x >> n
for left or right shift of x by n bits.
We want to define a single function
bitshift(x, n)
Before that, we have to determine, which shift is to be used for positive and negative n - what is the "sign" of each shift (or rotation) direction.
Is there a definition or convention for that?
(Please note that this question has nothing to do with signed/unsigned types)
UPDATES
Please also note that i am not asking for implementation details of this function, even it might be somewhat related..
There are similar functions in scheme/lisp-like languages, like ash, which do left shift for positive n
Since shifting right by k places is equal to multiplying by 2^-k, and shifting left is equal to multiplying by 2^k, I think that should give you a hint.
Note: Reason I would argue for this way of looking at it is that it is common to consider multiplication as more fundamental operation in some sense than division is, although you could certainly argue the other way around.
You can use negative number as argument
For example
x << n
so pass n=2 if yuo want to left shift two positions
and pass n=-2 if you want to right shift two positions.
I know the "<<" is a bit operation. but I do not understand what it exactly functions in TCL, and when should we use it?
can anyone help me on this?
The << operator in Tcl's expressions is an arithmetic bit shift left. It's exceptionally similar to the equivalent in C and many other languages, and would be used in all the same places (it's logically equivalent to a multiply by a suitable power of 2, but it's usually advisable to use a shift when thinking about bits and a multiply when thinking about numbers).
Note that one key difference with many other languages (from Tcl 8.5 onwards) is that it does not “drop bits off the front”; the language implementation automatically uses wider number representations as necessary so that information is never lost. Bits are dropped by using a separate binary mask operation (e.g., & ((1 << $numBits) - 1)).
There are a number of uses for the << shift left operator. Some that come to my mind are :
Bit by bit processing. Shift a number and observe highest order bit etc. It comes in more handy than you might think.
If you add a zero to a number in the decimal number system you effectively multiply it by 10. shifting bits effectively means multiplying by 2. This actually translated into a low level assembly command of bit shifting which has lower compute cycles than multiplication by 2. This is used for efficiency in the gaming industry. Shift if twice (<< 2) to multiply it by 4 and so on.
I am sure there are many others.
The << operation is not much different from C's, for instance. And it's used when you need to shift bits of an integer value to the left. This can be occasionally useful when doing subtle number crunching like implemening a hash function or deserialising something from an input bytestream (but note that [binary scan] covers almost all of what's needed for this sort of thing). For a more general info refer to this Wikipedia article or something like this, this is not really Tcl-related.
The '<<' is a left bit shift. You must apply it to an integer. This arithmetic operator will shift the bits to left.
For example, if you want to shifted the number 1 twice to the left in the Tcl interpreter tclsh, type:
expr { 1 << 2 }
The command will return 4.
Pay special attention to the maximum integer the interpreter hold on your platform.
How can I make a bitwise shift in MySQL? Is there any specific instruction or operator? If not, how to simulate it optimally?
Have a look at the bitwise operators in MySQL first: http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html
Then you have left shift:
http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html#operator_left-shift
And right shift:
http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html#operator_right-shift
To use a bitwise shift in MySQL, you can use << or >> for left shift or right shift respectively.
Looks like there is shift operators available, ie >> for right shift.
$ bc
BC> ibase=2
BC> 110&101 // wanna get 100
(standar_in) 8: syntax error
Wikipedia informs that the ops are "|, & and ^". It may be that they work only in certain BC-types or I misread something.
Those operators are listed in the section 'Missing' operators relative to C, which ends with "... are not available in POSIX bc"
Despite bc won't do it, you can use arithmetic expansion directly on the terminal if you use bash.
To XOR 44 and 61, you can do:
echo $((44^61))
If you want to use binary code, then:
echo $((2#110^2#101))
See Numerical Constants for changing the base.
See bitwise operators section to peep at available operators.
The wikipedia article is pretty clear that these ops aren't in either POSIX bc or gnu bc. The man page has no mention of them either.