We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a061f34 commit 338bf58Copy full SHA for 338bf58
runtime/doc/eval.txt
@@ -1138,9 +1138,10 @@ expr6 >> expr6 bitwise right shift *expr->>*
1138
*E1282* *E1283*
1139
The "<<" and ">>" operators can be used to perform bitwise left or right shift
1140
of the left operand by the number of bits specified by the right operand. The
1141
-operands must be positive numbers. The topmost bit (sign bit) is always
1142
-cleared for ">>". If the right operand (shift amount) is more than the
1143
-maximum number of bits in a number (|v:numbersize|) the result is zero.
+operands are used as positive numbers. When shifting right with ">>" the
+topmost bit (somtimes called the sign bit) is cleared. If the right operand
+(shift amount) is more than the maximum number of bits in a number
1144
+(|v:numbersize|) the result is zero.
1145
1146
1147
expr6 and expr7 *expr6* *expr7* *E1036* *E1051*
@@ -1417,6 +1418,10 @@ number number constant *expr-number*
1417
1418
Decimal, Hexadecimal (starting with 0x or 0X), Binary (starting with 0b or 0B)
1419
and Octal (starting with 0, 0o or 0O).
1420
1421
+Assuming 64 bit numbers are used (see |v:numbersize|) an unsigned number is
1422
+truncated to 0x7fffffffffffffff or 9223372036854775807. You can use -1 to get
1423
+0xffffffffffffffff.
1424
+
1425
*floating-point-format*
1426
Floating point numbers can be written in two forms:
1427
src/charset.c
@@ -2002,6 +2002,7 @@ vim_str2nr(
2002
}
2003
else
2004
{
2005
+ // prevent a larg unsigned number to become negative
2006
if (un > VARNUM_MAX)
2007
un = VARNUM_MAX;
2008
*nptr = (varnumber_T)un;
src/eval.c
@@ -3091,12 +3091,8 @@ eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3091
rettv->vval.v_number =
3092
rettv->vval.v_number << var2.vval.v_number;
3093
3094
- {
3095
3096
- rettv->vval.v_number >> var2.vval.v_number;
3097
- // clear the topmost sign bit
3098
- rettv->vval.v_number &= ~((uvarnumber_T)1 << MAX_LSHIFT_BITS);
3099
- }
+ (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
3100
3101
3102
clear_tv(&var2);
src/testdir/test_expr.vim
@@ -958,6 +958,8 @@ func Test_bitwise_shift()
958
call assert_equal(0, 0 >> 4)
959
call assert_equal(0, 999999 >> 100)
960
call assert_equal(0, 999999 << 100)
961
+ call assert_equal(-1, -1 >> 0)
962
+ call assert_equal(-1, -1 << 0)
963
VAR a = 8
964
VAR b = 2
965
call assert_equal(2, a >> b)
@@ -976,6 +978,15 @@ func Test_bitwise_shift()
976
978
for i in range(0, v:numbersize - 2)
977
979
LET val = and(val, invert(1 << i))
980
endfor
981
+ #" -1 has all the bits set
982
+ call assert_equal(-2, -1 << 1)
983
+ call assert_equal(-4, -1 << 2)
984
+ call assert_equal(-8, -1 << 3)
985
+ if v:numbersize == 64
986
+ call assert_equal(0x7fffffffffffffff, -1 >> 1)
987
+ call assert_equal(0x3fffffffffffffff, -1 >> 2)
988
+ call assert_equal(0x1fffffffffffffff, -1 >> 3)
989
+ endif
990
call assert_equal(0, val)
991
#" multiple operators
992
call assert_equal(16, 1 << 2 << 2)
src/version.c
@@ -734,6 +734,8 @@ static char *(features[]) =
734
735
static int included_patches[] =
736
{ /* Add new patch number below this line */
737
+/**/
738
+ 5004,
739
/**/
740
5003,
741
src/vim9execute.c
@@ -4096,12 +4096,7 @@ exec_instructions(ectx_T *ectx)
4096
case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4097
res = 0;
4098
4099
4100
- res = arg1 >> arg2;
4101
4102
- res &= ~((uvarnumber_T)1
4103
- << MAX_LSHIFT_BITS);
4104
+ res = (uvarnumber_T)arg1 >> arg2;
4105
break;
4106
default: break;
4107
src/vim9expr.c
@@ -2719,11 +2719,8 @@ compile_expr5(char_u **arg, cctx_T *cctx, ppconst_T *ppconst)
2719
else if (type == EXPR_LSHIFT)
2720
tv1->vval.v_number = tv1->vval.v_number << tv2->vval.v_number;
2721
2722
2723
- tv1->vval.v_number = tv1->vval.v_number >> tv2->vval.v_number;
2724
2725
- tv1->vval.v_number &= ~((uvarnumber_T)1 << MAX_LSHIFT_BITS);
2726
+ tv1->vval.v_number =
+ (uvarnumber_T)tv1->vval.v_number >> tv2->vval.v_number;
2727
clear_tv(tv2);
2728
--ppconst->pp_used;
2729
0 commit comments