That's the bitwise left shift operator Basically it's doing this // moves 0 bits to left for 00000001 let birdCategory: UInt32 = 1 << 0 // moves 1 bits to left for 00000001 then you have 00000010 let worldCategory: UInt32 = 1 << 1 // moves 2 bits to left...

java,bit-shift,randomaccessfile

A RandomAccessFile "works" on a physical file, it reads from and writes to a file. You can't just insert bytes into a position. All the bytes after the position have to be moved, copied to allow space for the value you want to insert. The most efficient way would be...

Shifting a number to the left is equivalent to multiplying that number by 2n where n is the distance you shifted that number. To see how is that true lets take an example, suppose we have the number 5 so we shift it 2 places to the left, 5 in...

java,biginteger,bit-shift,ordinal

BigInteger b =BigInteger.ZERO; b = b.setBit( ordinal() ); You may also use BigInteger.and(BigInteger x). enum OneTwo { one, two, three; BigInteger biValue() { BigInteger bi = BigInteger.ZERO; return bi.setBit(ordinal()); } BigInteger biValue( BigInteger filter ) { BigInteger bi = BigInteger.ZERO; return bi.setBit(ordinal()).and(filter); } } And a test: BigInteger three...

I am not sure why you need to do the bit shift approach. Consider simply casting a very large -1 to your unsigned type as shown by LARGEST_VALUE_3 below. I think you were going for LARGEST_VALUE_2 and LARGEST_VALUE_1 is simply wrong (but was suggested earlier). #include <stdio.h> #include <stdint.h> typedef...

A halfway decent compiler will tell you: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘short int *’ scanf("%d", &curVal); You need to use %hd to scan a short, or (in this case better) change your variable to int. If you're using GCC, add...

c++,algorithm,c++11,optimization,bit-shift

In C++, you can use std::minmax to produce a std::pair of the minimum and the maximum. This is particularly easy in combination with std::tie: #include <algorithm> #include <utility> int largest, secondLargest; std::tie(secondLargest, largest) = std::minmax(arr[0], arr[1]); GCC, at least, is capable of optimizing the call to minmax into a single...

When you use the _mm_set_epi* functions, they accept their parameters as the most significant item first. For example, the first statement, __m128i _16 = _mm_set_epi8( 128, 64, 32, 16, 8, 4, 2, 1, 128, 64, 32, 16, 8, 4, 2, 1); will load the variable with this value: 0x80402010080402018040201008040201 (128,64,32...

Start with the baseline, 0xaf is 1010-111116 (and we're assuming an eight-bit char here based on the code though it's not mandated by the standard). The expression value << 4 will left-shift that by four bits (not one as you seem to think), giving 1010-1111-000016 and, yes, it's more than...

It's fully equivalent, but it was written this way to make it clearer where the value came from and how it was computed -- in this case that it was intended to be 2^4. It could have been written either way, the authors just thought this would be more self-documenting....

Nope, there's no binary arithmetic in Freemarker out-of-the-box. (Of course, all binary operations can be implemented with the four basic operators, but it would be rather perverse to do... Though shift left with * 2 and % doesn't sound that bad.)

Shift operations are performed using a CPU register. The register consists of a number of bits (8, 16 and 32 are common, and you appear to have a 32 CPU), which in combination can be interpreted as a decimal value. In your example you use the value 1. The C...

algorithm,cryptography,bit-shift,polynomial-math,ecdsa

There is no generic answer to this question: you can use portable code that will be tedious to optimize or highly machine specific code that will be even more complicated to optimize without breaking. If you want real performance, you have to use MMX/AVX registers on the maximum width you...

#define CHAR_BITS ( 8 ) #define UINT_BITS ( sizeof(unsigned int) * CHAR_BITS ) // We need modulo, not remainder. This is a way to obtain it. int mod(int x, int m) { return ((x % m) + m) % m; } // Calculate left rotation for any shift value. unsigned...

Right shift on an unsigned quantity will make new zeros to enter, not ones. Note that right shift is not a right rotation. To do that you need x = (x >> 1) | (x << 7) ...

performance,cryptography,bit-manipulation,bit-shift,javacard

When it comes for speed, known length, hard-coded version is the fastest (but ugly). If you need to shift more than one bit, ensure to update the code accordingly. output[0] = (byte)((byte)(input[0] << 1) | (byte)((input[1] >> 7) & 1)); output[1] = (byte)((byte)(input[1] << 1) | (byte)((input[2] >> 7) &...

c#,unity3d,bit-shift,bitmask,raycasting

Manipulating individual bits is mainly done using the &, |, ~ and <</>> operators. Example (with bytes): // single value byte a = 1; // 00000001 // shift "a" 3 bits left byte b = a << 3; // 00001000 // combine a and b with bitwise or (|) byte...

What I think you need is a solid explanation of bitmasking. For this particular case, you should use the & operator to mask out the upper 16 bits of c->isize before shifting it up, and then use the & operator again to mask the upper 48 bits of c->barc. Let's...

c++,32bit-64bit,bit-shift,unsigned-integer

unsigned long is only guaranteed to have 32 bits. See here. You need to use unsigned long long to have 64 bits guaranteed. Even better would be to use a fixed width integer, i.e. uint64_t. They are defined in header <cstdint> (or <stdint.h>)....

c,binary,bit-manipulation,bit-shift

Left shifting it twice would give 1000 0000)2 = 128)10. If 128 is representable in char i.e. you're in some machine (with a supporting compiler) that provides a char of size > 8 bits then 128 would be the value you get (since it's representable in such a type). Otherwise,...

The reason the default Integer.toBinaryString() doesn't prepend any zeros is because that notation could be confused with the octal notation as mentioned by Hans. If you want to see all the digits you will have to write your own toString function. Below is the one I slightly changed from javas...

Yes, it's possible to do so. I would use an appropriate union to mask the value regions: union mydate_struct { struct { uint16_t day : 5; // 0 - 31 uint16_t month : 4; // 0 - 12 uint16_t year : 7; // 0 - 127 }; uint16_t date_field; };...

c,bit,compiler-optimization,bit-shift

That code is very complicated just to check a bit in an integer. Try the standard method: return (t & (1U << n)) != 0; If you have to check n is valid, add an assertion. else masking (n & 7) or modulus (n % 8) (this will be optimized...

c++,optimization,bit-shift,multiplication

This question has been bugging me for the past few days, so I decided to do some more investigation. My initial answer focused on the difference in data values between the two tests. My assertion was that the integer multiplication unit in the processor finishes an operation in fewer clock...

Checking bits by numeric position is one of the correct ways to do so but it makes the code depend on magic numbers, which makes it harder to read and maintain. Usually, when checking for bitmasks, there is a goal of checking for some specific flag, let's say a hardware...

javascript,bit-manipulation,unsigned,bit-shift,integer-overflow

The shift operators always effectively has a right operand in the range 0-31. From the Mozilla docs: Shift operators convert their operands to 32-bit integers in big-endian order and return a result of the same type as the left operand. The right operand should be less than 32, but if...

Based on your code example, it appears that the only thing you have left remaining to do is to shift the 8-bit samples left 8 bits, so that the data occupies the most-significant-bits of the 16-bit sample. I.e.: Dim buffer((44100 * 2) - 1) As Byte ' place to dump...

Its a left shift operator. It is shifting the bits in the number 1 to the left by zoom number of places. In the context of this 1 << zoom, it is basically creating a power of 2. If zoom equals 3, then its doing a 2^3: 0000 0001 =...

c#,bit-manipulation,xor,bit-shift

if you look on Convert.ToByte() documentation, the only exception it produces is actually OverflowException cause value is less than Byte.MinValue or greater than Byte.MaxValue. and p5 in your code becomes 349. To resolve this: 1) as mantioned in comments operate on byte type and do not convert it into the...

delphi,bit-shift,delphi-xe7,livebindings

You have to use a BindSource for the binding. In this example I just use the TPrototypeBindSource. There is no need to have an component for the data object, a simple object is enough. unit DataObject; interface type TBits2Byte = class private { Private declarations } fBit00, fBit01, fBit02, fBit03,...

%d prints the int version of the unsigned int i. Try %u for unsigned int. printf("%u\n", i); int main(){ printf("%d, %u",-1,-1); return 0; } Output: -1, 4294967295 i.e The way a signed integer is stored and how it gets converted to signed from unsigned or vice-versa will help you. Follow...

Maybe something like this using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { StreamReader reader = new StreamReader("filename"); UInt16 chr = (UInt16)reader.Read(); UInt16 word = 0; int shift = -1; while (shift == -1) { word = (UInt16)((UInt16)(word...

Am I doing it right ? No. Convert.ToUInt64((1 << (j % 64)) converts the result of shifting, not the argument. what is the C# equivalent for 1i64 You may use 1L instead of 1i64, or 1UL instead of 1ui64. BTW, be aware that in some cases left shifting of...

The bitwise or will totally ruin the parameters, just shift them instead and then take the required bits: public static byte setParameters(int b1, int b2, int b3, int b4) { byte result = (byte) b1; result |= (b2 << 1) & 0x02; result |= (b3 << 2) & 0x0c; result...

var a = u32timestamp&0xFF; var number= u32timestamp >>8; var en = a-16; the coding place "number" shifting eight bit to the left, so a regular 8 shift on the right will recover it by trashing the LS bits. Then we mask the lower bit to recover en....

The thing is you are not considering negative numbers representation correctly. With right shifting, the type of shift (arithmetic or logical) depends on the type of the value being shifted. If you cast your value to an unsigned value, you might get what you are expecting: int16_t b = ((unsigned...

There's no operator but you can do: std::rotate(myarray, myarray + N, myarray + 4); where N is the index of the element that should be moved to the start, and the other arguments are the begin and end. You could overwrite later elements afterwards if you wanted. In your case:...

Is this Go code correct and portable It is correct, as long as you work with unsigned integers of 64-bit width. Portability in this case is provided by the sync/atomic packages to the architectures supported by the Go compiler. Note, however, that not all architectures support "true" atomic operations...

c#,.net,bit-manipulation,bit-shift

One way might be something like: ulong mask = ((1 << length) - 1) << offset; I'm not clear why you even want the bitoffset value, but that should simply be a matter of shifting by your offset value, right? E.g.: ulong bitoffset = 1 << offset; ...

For some reason it's not included in Data.Bits, but there's a GHC primop: uncheckedIShiftRL#. Also, GHC.Base exports a safer version as iShiftRL#: iShiftRL# :: Int# -> Int# -> Int# a `iShiftRL#` b | isTrue# (b >=# WORD_SIZE_IN_BITS#) = 0# | otherwise = a `uncheckedIShiftRL#` b We can wrap either the...

java,bit-manipulation,bit,bit-shift

Can be achieved using "masking" creating a mask for the position i to j with each bit set to 1 using bitwise OR incrementally blank out the bits in N using bitwise AND and bitwise NOT of the mask select the bits from M using mask with bitwise AND copy...

A signed bit shift doesn't just shift the bits for negative numbers. The reason for this is you would get the wrong result. -1 in twos complement has all the bits set i.e. it's: 111111...1 If you were to just shift this you'd get: 011111....1 In two's complement this would...

assembly,output,bit-shift,bits,lc3

You're forgetting that OUT prints the character that the ASCII value points to. You would need to increase your answer by 48. You'll then run into problems when it comes to numbers of bits over 9, so you'd need to do some checking there. Hope that helps!...

You're casting a non-pointer value to a pointer: (byte_pointer) x and then dereferencing it, this won't work. Try to take the address of x and cast that: (byte_pointer) &x instead. Make this change and your program will run, but be aware that casting an int to an unsigned char might...

If your system has 32-bit or smaller int, then you cause undefined behaviour by shifting by 32 bits or more. Shifts must be by a fewer number of bits than the type has. If you have 64-bit int then 0xFFFFFFFF will be a signed int, and shifting it by 32...

You have a combination of things that are acting against you. float simulated_fp_add(float, float); c = simulated_fp_add( a, b ); u.f = c; You are calling a function that takes floats with integer values of floating point bit patterns. These are being converted to (garbage) floating point values before the...

c++,bit-manipulation,bit-shift,bitwise-or,bitboard

You need to use 1LL as 64 bit value before you use shift operator << to get 64 bit result: #include <stdint.h> uint64_t kings = 0ULL; kings |= 1ULL << i; ...

Besides the problems noted by @SouravGhosh, you need to (5) convert from the digits 0/1 to the character values for '0' and '1' by adding in the value of the character '0' [actually Sourav noted that too], and (6) pick off the low-order bit before shifting k right by 1...

All bitwise operations in Java are done using ints or longs, therefore you don't have too much choice here; and casting a shorter primitive type whose sign bit is set will "expand" the sign bit to all "newer created" bits. There is a solution to do it "in one line",...

The code assumes a 32 bit integer and 8 bit bytes. A 32 bit integer is made up of 4 bytes: Let's say these 4 bytes are laid out in memory like so: +---------------------------------+ |Byte 4 | Byte 3 | Byte 2 | Byte 1| +---------------------------------+ This could relate to...

You should do it yourself. You are the one declaring what is the semantic of the operators, bison had no idea what it could be. What if you choose to implement + as printf("error") and * as exp1.erase(exp2)? Is x+x equals 2*x in your language? Probably not. Will optimization make...

java,reverse-engineering,bit-shift

You can reverse it with : X = (config >> 6) & 0b11 Y = config & 0b11111; & 0b11 and & 0b11111 are bitwise operations. They respectively gives you the last 2 bits and the last 5 bits of the number they are apply on. What about arbitrary lengths...

compression,glsl,bit-shift,glsles,bit-packing

Well, bit shifting can be represented by mulitplication (for left shifts) or division (for right shifts) with powers of two. You just have to take into account that the floats will stroe the fractional parts which would normally be shifted "out" in normal integer bitshifts. So to pack 4 normalized...

The main problem with the current setup, is that it doesn't check if the shift causes bits to go 'off'. Using a byte as example, the number 12 would be: 00001100 Meaning the maximum number of bytes that could be shift to the left is 4. Vice versa, using your...

c,bitwise-operators,avr,bit-shift,unsigned-integer

Another problem besides the formatting pointed out by Sourav, is that the value 1 is an int, and it's unlikely that int is 64 bits (in fact, it seems to me that on your platform int is only 16 bits). You need to use e.g. 1ULL to get an unsigned...

I need to do the same but in reverse. I want to make 4 chars from one int. Just do the operations in reverse. Shift to right instead of left and mask after shift instead of before: char bytes[4]; long number = 0x00000190L; // int may be only 2...

c,bit-manipulation,bitwise-operators,bit-shift

A good way to understand bitmasks is with an example so I will give one. Lets say we have an array of structs: struct my_struct { int foo; int bar; }; struct my_struct array_of_structs[64]; // I picked 64 for a specific reason I will discuss later We will use this...

c#,concatenation,bit,bit-shift,uint16

I already tried to (logically) bit-rightshift r[0] by 8, but then the upper bits get lost because they are stored in the first 8 bits of r[1]. Well they're not "lost" - they're just in r[1]. It may be simplest to break it down step by step: byte val1LowBits...

The author did not clarify this, but apparently it is not intended to be an arithmetic shift...

I feel that the other answers are a bit incomplete. It's true that an int is 32 bits, and the language doesn't let you shift more than 32 bits. What's gotten left out is that if you tell it to shift more than 32 bits, the shift amount will be...

To start with, 61 is only equal to 00110110 00110001 if you store it as two ASCII digits. When written in binary, 61 is 111101. Note that the binary representation requires six binary digits, or six "bits" for short. That's what we're taking advantage of in this line: Color =...

Although your x is of type long int, the 1 is not. 1 is an int, so 1<<63 is indeed undefined. Try (static_cast<long int>(1) << 63), or 1L << 63 as suggested by Wojtek....

perl,vector,bit-shift,pack,unpack

The size of your integers are given (in bytes) by perl -V:ivsize or programatically using use Config qw( %Config ); $Config{ivsize}. They'll have 32 bit in a 32-bit build (since they are guaranteed to be large enough to hold a pointer). That means you can use my $i = ($hi...

Take a look at the BitCoin documentation. vRecv is an instance of CDataStream which overloads the operator>> to read and unserialize data. Background To understand the expression, precedence and associativity of operators are important. In C++, the >> operator is left-associative, which means you can rewrite your expression (vRecv >>...

image,matlab,image-processing,histogram,bit-shift

Calculating Indices The bitshift operator seems OK to do. Me what I would personally do is create a lookup relationship that relates RGB value to bin value. You first have to figure out how many bins in each dimension that you want. For example, let's say we wanted 8 bins...

Here's one (not very elegant) way: byte[] bytes = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe}; // now shift left byte b0 = bytes[0]; System.arraycopy(bytes, 1, bytes, 0, bytes.length -1); bytes[bytes.length - 1] = b0; ...

c++,bitwise-operators,bit-shift

This is due to operator overloading. cout <<(11>>1)<<1<<endl; // ^ output operator // ^ right shift // ^ output operator If you were to change the code to this, then your answer would be correct: cout << ((11>>1) << 1) <<endl; // brackets force left shift operator instead of output...

use brackets: + has higher precedence over <<. var a = (0x0028 << 16) + 0x0010 // note the brackets. Refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence...

Since you are starting the computation at the least significant bit end and progress toward the most significant bit end as you go, you need an operation to place a bit in n-th position, as opposed to shifting in a bit*. Placing n-th bit in an initially zeroed value is...

c,bit-manipulation,bitwise-operators,bit-shift

Two things: image[i] & mask1 only checks whether image[i] and mask1 have at least one bit in common. Is this your intention? first_hline_first_row &=~ (0<<i); does nothing (~(0<<i) is all ones). From the comments I'm guessing that this is what you meant to write: for(i=0; i<16; i++){ if(image[i] & mask1)...