c,bitmap,bitwise-operators,bmp,little-endian

The problem actually wasn't in the shifts, for the number 1200 something goes from in this line: new_int += pos1; There pos1 is implicitly converted to an int, and since it is a signed char with value 0xB0 (1200 = 0x4B0), it is interpreted as negative. That shows up as...

bit-manipulation,bit,bitwise-operators,hamming-distance

You can simply do this : int need=__builtin_popcountll(A^B); cout<<need; ...

c,integer,bitwise-operators,literals

Somehow, it looks like the ~ operator already knows that it should act on 64 bits? It's not the ~ operator, it's the cast. Here is how the integer conversion is done according to the standard: 6.3.1.3 Signed and unsigned integers When a value with integer type is converted...

c,arrays,bit-manipulation,bitwise-operators

They are just characters... char *shift_right(unsigned char *ar, int size, int shift) { memmove(&ar[shift], ar, size-shift); memset(ar, '0', shift); return(ar); }; Or, convert the string to a long-long, shift it, then back to a string: char *shift_right(char *ar, int size, int shift) { unsigned long long x; char *cp; x=strtoull(ar,...

boolean isSectorOne (int val) {return (val & 0x01) == 0x01;} boolean isSectorTwo (int val) {return (val & 0x02) == 0x02;} boolean isSectorThree (int val) {return (val & 0x04) == 0x04;} boolean isSectorFour (int val) {return (val & 0x08) == 0x08;} ...

bit-manipulation,bitwise-operators

Yes, but not like that. Observe the values of option that you get to see why - as soon as you AND together two sub-queries, everything goes wrong. For arbitrary queries, you will need at least a list of options (each of which is also a combination), either in Sum...

java,android,bit-manipulation,bitwise-operators

I assume that you want to perform a bitwise operation on strings(which performs a bitwise operation on ASCII values of characters of these string in PHP). There is no such operator for String in Java, but you can do it using BitSet: public String or(String a, String b) throws UnsupportedEncodingException...

php,mysql,bit-manipulation,aggregate-functions,bitwise-operators

108543 is what you get when you or together the decimal values {1, 10, 100, 1000, 10000, 100000}. In other words, they're not being treated as binary values. You either need to store the correct decimal values for the binary equivalents, { 1, 2, 4, 8, 16, 32}, or find...

To set bits you need |: temp = temp | 0x3; or more compactly: temp |= 0x3; If you want to set a 3-bit span to the number 3, you need to both set and clear bits: temp &= ~0x7; // clear bits temp |= 0x3; // set bits ...

java,operators,bitwise-operators

It is a bit shift operation. Read more here. It will pack these 3 numbers into one integer.

javascript,operators,bit-manipulation,bitwise-operators

The operands of bitwise operations are always converted to signed 32-bit integers in big-endian order and in two's complement format. That would be 00000000000000000000000000000101 or 00000000000000000000000000000000 ------------------------------------ 00000000000000000000000000000101 ...

sql,sql-server,sql-server-2008,bitwise-operators

T-SQL has no bit-shift operators, so you'd have to implement one yourself. There's an implementation of a bitwise shifts here: http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/bitmask-handling-part-4-left-shift-and-right-shift.aspx You'd have to cast your integer to a varbinary, use the bitwise shift function and cast back to integer and (hopefully) hey-presto! There's your result you're expecting. Implementation and...

python,shell,bit,bitwise-operators

You haven't told Python that you're entering binary numbers; like C, Python will interpret 0010000000000000 as octal (8**13), and 1011110110011001 as decimal. Use the 0b prefix and bin function instead: >>> a = 0b1011110110011001 >>> b = 0b1100001110000101 >>> bin(a|b) '0b1111111110011101' Your 8-bit (Python doesn't know that either, int is...

Your prime() function exhibits undefined behavior. If it doesn't find a prime, the final conditional will also be false (because a is near the sqrt(n), not near n), which means you will have no executed return statement. To fix this, Just return 1 if you complete the for loop...

It won't compile, since it is escaped HTML entities, < is the same as < and > is the same as > lt stands for less than gt stands for greater than amp stands for ampersand ...

if-statement,operators,bit-manipulation,conditional-statements,bitwise-operators

It loops over all values from 0 to n, and for each of these: It loops over each bit in the value. if the value is set: It executes //code Lets examine the complex part: if(i & (1<<j)) 1<<j is a common way to set the jth bit (starting from...

c++,bitwise-operators,unsigned-integer,integer-promotion,signed-integer

[expr.unary.op] The operand of ~ shall have integral or unscoped enumeration type; the result is the one’s complement of its operand. Integral promotions are performed. [expr.shift] The shift operators << and >> group left-to-right. [...] The operands shall be of integral or unscoped enumeration type and integral promotions are performed. What's...

bitwise-operators,logical-operators,equation-solving,bitwise-xor

In each iteration of the loop you are effectively calculating c_i = e ^ ( p + c_(i-1) ) If you wish to reverse the loop then given a c_i you need to calculate c_(i-1) However as you said xoring twice gets you back to the original value and xor...

python,python-3.x,bit-manipulation,bitwise-operators

From here ~x Returns the complement of x - the number you get by switching each 1 for a 0 and each 0 for a 1. This is the same as -x - 1. Following the last part of that statement: -1 - 1 does indeed equal -2 and -0...

objective-c,c,bitwise-operators

You can use regular arrays and interpret them as bit arrays. Let's say you use an array of chars, so every element has 8 bits. If you want to access bit 231, you will find it in 231/8=28th element of the array (the 231%8=7th bit of that array). This is...

c,hex,overflow,bitwise-operators

The -~y does have the same issue, the compiler just doesn't detect it statically and therefore doesn't warn you about it. When using the integer literal directly, the compiler does the calculation at compile time and realizes that there is an overflow.

c++,variables,bitwise-operators,xor

This is a compiler specific question, but I tried it with g++ -O2 and clang++ -O2. It compiled this: int main (int argc, char** argv) { int a = argc, b = argc * 3; a ^= b; return a; } to leal (%rdi,%rdi,2), %eax xorl %edi, %eax ret The...

c,bit-manipulation,bitwise-operators,bitwise-and

& is the bitwise AND operator. here, (x & z) == z means, perform a bitwise AND of x and z and if that value equals to z, then.... Ref: Chapter 6.5.10, C11 standard, "Bitwise AND operator" The result of the binary & operator is the bitwise AND of the...

c++,bit-manipulation,bit,bitwise-operators

From Wikipedia: The bitwise AND operator is a single ampersand: &. It is just a representation of AND which does its work on the bits of the operands rather than the truth value of the operands. Bitwise binary AND does the logical AND (as shown in the table above) of...

c,bit-manipulation,bitwise-operators,logical-operators

Where you're off is in #2: if x is positive, x >> 31 == 0 and !x == 0 so !(0 | 0) == 1 if x is negative, x >> 31 == 1 and !x == 0 so !(1 | 0) == 0 if x is zero, x >>...

result = (a & b) | (b & c) | (c & a); ...

c,math,bit-manipulation,bitwise-operators

I would use this as your getAmountFromBitwise(). int highest_bit_set(long long n) { int result = 1; while(n >>= 1) /* keep shifting right until n == 0 */ result++; return result; } Note that this requires that n != 0 for a correct result....

java,sockets,networking,bit,bitwise-operators

You can cast it to byte : byte toSend = (byte)0xFF; Forget the other part of my answer. I'm a bit sleep deprived. Actually it's -1 that is represented as 11111111. But (byte)0xFF works just fine....

javascript,python,bitwise-operators

BITWISE AND Each item in row 1 is multiplied by the corresponding value in row 2: a = 5 # In binary: 0101 b = 3 # In binary: 0011 c = a & b # In binary: 0001 print c # In decimal: 1 BITWISE OR If any item...

c,performance,bit-manipulation,bitwise-operators,bitvector

14 operations for all masks. The idea is to first sort the bits, using min = x & y and max = x | y as conditional swap. This costs 10 operations. Then simply extract the masks which costs 4 operations. // Split in lower and upper half var c1...

c,bit-manipulation,bitwise-operators,firmware

It's not possible in a single instruction. This is because there are 3 possible operations you need to do on the different bits: Set them (bit 3) Clear them (bit 4) Leave them alone (all the other bits) How can you select from one of three possibilities with a bitmask...

The reason (char)((unsigned char)~0 >> 1) is 127 is obvious - You have 0xFFFFFFFF, cast it to unsigned char, you get 0xFF, shift by one and you have 0x7F or 127. The weird thing is why the first one is wrong: You cast a zero into unsigned char. Then you...

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)...

From the MDN: The operands of all bitwise operators are converted to signed 32-bit integers in two's complement format. All numbers in JavaScript are IEEE754 double precision floating point numbers. They're converted to 32 bits integers before being applied bitwise operators. More precisely, this process is described in ToInt32 in...

There is no built in bitwise-or assignment operator in Kotlin (yet).

c#,enums,bitwise-operators,bitflags

That looks fine, though for flags you must remember you cannot increment by ones (1, 2, 3, 4) - it must be done like: 1, 2, 4, 8. Using your enum definition: [Flags] public enum Roles : byte { View = 1, Edit = 2, Admin = (View | Edit)...

The << and >> operators are bit-shift operators; they change the value based off of the binary representation of the number; an example will clarify: 001010 (10) If we do << 1 (left shift 1 bit), then we get: 010100 (20) If you noticed, the above is equivalent to multiplying...

[(a*x)^b]*c=d [(a*x)^b]=d/c (a*x)^b^b=(d/c)^b //double xor with b retrieves initial value (a*x)=(d/c)^b x = ((d/c)^b) / a ...

Odd numbers in binary always have a least-significant bit (LSB) of 1. That is why your code function odd($var){ return ($var & 1); } returns true on odd numbers. Here are your examples from your question: (decimal) 4 & 1 = (binary) 100 & 001 = (binary) 000 = (decimal)...

c,syntax,bit-manipulation,bitwise-operators

Shift operator only only works on integral types. Using << causes implicit integral promotion, type casting b to an int and "protecting" the higher bits. To solve, use temp = ((unsigned char)(b << 4)) >> 4;...

c,bit-manipulation,bitwise-operators,c11

My question is this: why is it that an error is returned for a raw number, but not for any of the variables? Because absence of compiler warnings is not a guarantee of good program behavior. The compiler would be right to emit a warning for a << x,...

javascript,object,methods,typeerror,bitwise-operators

The problem is that the methods in the class are changing the global state variable that you used to hold the instance of the class, instead of changing the property in the class. The first call overwrites the variable, so the second call fails as the variable no longer contains...

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,function,operators,bitwise-operators,toupper

Following the ASCII table, to convert a letter from lowercase to UPPERCASE, you need to subtract 32 from the ASCII value of the lowercase letter. For the ASCII values representing the lowercase letters, subtracting 32, is equal to ANDing ~32. That is what being done in *(str + index) &=...

In your code, the type of 1 is (as always) int. So, if sizeof (int) is smaller than sizeof (long), it stands to reason that you can't shift an int over all the bits in a long. The solution is of course to use an (unsigned) long constant: data |=...

java,bitwise-operators,bitwise-and

Your assumption is correct. It's to remove the sign of the integer in case the hash is negative. ANDing with Integer.MAX_VALUE will remove the sign bit from the integer. Note that this is different from getting the absolute value of the integer: int hash = -1; int caller = (hash...

c#,performance,bitwise-operators,bitwise-and

The main problem here is almost certainly the allocation of the List, and the allocation needed when you are inserting new elements, and when the list is converted to an array in the end. This code probably spend most of its time in the garbage collector and memory allocator. The...

javascript,bitwise-operators,twos-complement,ones-complement

Note - I don't think this is the problem, but it's a problem. You've left off a set of { } else { document.getElementById("binary").innerHTML = aNumber.toString(2); document.getElementById("ones").innerHTML = (~aNumber).toString(2); document.getElementById("twos").innerHTML = (-aNumber).toString(2); } Without those, the second two lines were outside the scope of influence of the if else statement,...

javascript,php,bitwise-operators,xor,base

2387507830 == -1907459466 when using unsigned integers (look at the bit values of the least significant bits) 2387507830 = 0000 0000 0000 0000 0000 0000 0000 0000 1000 1110 0100 1110 0111 1010 0111 0110 -1907459466= 1111 1111 1111 1111 1111 1111 1111 1111 1000 1110 0100 1110 0111 1010...

php,mysql,bit-manipulation,bitwise-operators

You can perform bitwise operations on integer columns but you still need to store them as decimal values. To store decimal 2 in an integer column store 2, not 10. You might want to use the bit column type instead. Either way, you can use b'value' notation to write the...

arrays,bitwise-operators,bitwise-and,mql4

The &in the argument indicates pass-by reference and not bitwise and. Passing by reference as opposed to pass-by value means that the argument value isn't copied, instead it's the address to the data structure that is copied and passed in to the function. Edit: see @Sneftel's comment below about pass-by...

c,bit-manipulation,bitwise-operators,stm32

Bits are 0-indexed, but you coded the shifts as if bits were 1-indexed instead. You have to use 0 through 7 for the shifts on a uint8_t. Also, the right-shifts can be removed. Try this: void FP_UpdateData(FP_BUTTONS *data, uint8_t b1, uint8_t b2) { data->button_1 = (b1 & ( 1 <<...

As the others have noted, this is undefined behavior, which means that if the C standard is all you have to go by, in theory anything could happen. I think that's a somewhat unhelpful answer to your question though, as you're left wondering why the same operation on the same...

c++,optimization,bit,bitwise-operators

The quick answer to your question is: there is no way to do this efficiently. The long answer is actually a series of new questions: what do you really intend to achieve with this? What do you even mean exactly by shifting one bit at the beginning of a file?...

objective-c,enums,bitwise-operators

The expression 1<<2 means to shift the bits for the value 1 to the left 2 places. Think of the << as arrows pointing left. The value 1 in binary (8-bits): 00000001 Now shift the bits to the left 2 places: 00000100 That's why 1<<2 changes the value 1 to...

objective-c,c,bit-manipulation,bitwise-operators

This value is not binary. It is octal. uint8_t sizeOfGlobalColorTable = 010; In (Objective) C constants starting from 0 are interpreted as octal values. What you actually write is b1000 & b0111 = 0. It should be: uint8_t sizeOfGlobalColorTable = 0x2; ...

javascript,type-conversion,byte,bitwise-operators

the classic would be var sbyte = (byte & 127) - ((byte & 128) ? 128 : 0); Or var sbyte = (byte & 127) - (byte & 128); ...

First: nr += x % 2; Imagine x in binary: ...1001101 The Modulo operator returns the remainder from a / b. Now the last bit of x is either a 0, in which case 2 will always go into x with 0 remainder, or a 1, in which case it...

The division operator / simply divides the left hand side by the right hand side. In this case the right hand side is 2, so it's shifting a-1 right by one bit (an arithmetic shift, which sign-extends the integer). The whole expression therefore calculates - for a>=0 - whether a...

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...

python,operators,bitwise-operators

>> is the right bit-shift operator, and & is bitwise AND. This is easier to see if you look at numbers in binary form. >>> format(13, "08b") '00001101' >>> format(13 >> 1, "08b") '00000110' You can see that the binary digits are right-shifted by one place (this is equivalent to...

javascript,bit-manipulation,bitwise-operators

As you hint in your tags, you can use bitwise operators if n = index of your bit and b = your bit string then b & ~(1 << n) will always have the nth bit as 0...

php,int,bit-manipulation,bitwise-operators

Following shift trick will do it. function bitnum($num){ return ~(~0<<$num) } Check the Fiddle...

Converting the string to int is easy: start with a zero int, then go through the characters of the binary representation, and check if it is zero or one. First, shift the partial result to the left. Then, if the digit is one, OR the result with 1. Continue to...

c++,enums,bitwise-operators,member,bitmask

This is not an OR operator for enum dlgFlags, instead it's an operator|= for DialogMenu (having 3 operands including *this, so it doesn't compile): dlgFlags operator|=( dlgFlags first, flgFlags second) { return (dlgFlags)( (unsigned)first | (unsigned)second ); } Change it to friend dlgFlags operator|(dlgFlags first, dlgFlags second) { return (dlgFlags)(...

You have to do the manual bit shifting and OR'ing, but your code can be cleaned up by removing all those buffer.Bytes() calls. You also do not need the &0xff parts. What n&0xff does, is clear out all bits outside of the 0-255 range. Since each value in the buffer...

java,php,bit-manipulation,bitwise-operators

Let's make php use char also: $res = array("\0", "\0", "\0", "\0"); // instead of $res = array(0,0,0,0); $res[$j] = ((~$res[$j]) & "\xff"); // instead of $res[$j] = ((~$res[$j]) & 0x00ff); $strCheckSum = $strCheckSum . bin2hex($res[$i]); // instead of $strCheckSum = $strCheckSum . dechex($res[$i]); ...

arrays,set,bit-manipulation,bitwise-operators

You can look at it from a different perspective: pick a power of two. Can we generate it? This question is easy to answer. Take all items from the set in which the bit corresponding to the power of two is set. Calculate the AND of all of those. The...

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...

c,bit-manipulation,bitwise-operators

It is checking whether the bth bit of a is set. 1<<b will shift over a single set bit b times so that only one bit in the bth position is set. Then the & will perform a bitwise and. Since we already know the only bit that is set...

javascript,php,bitwise-operators

In PHP you need to cast manually to integer function myFunc($a, $b, $c) { return ((int)$a ^ ((int)$b | (~(int)$c))); } ...

c++,binary,bit-manipulation,bitwise-operators

You can get a 4-bit nibble at position k using bit operations, like this: uint32_t nibble(uint32_t val, int k) { return (val >> (4*k)) & 0x0F; } Now you can get the individual nibbles in a loop, like this: uint32_t val = 1265985549; for (int k = 0; k !=...

python,binary,bitwise-operators

You seem to assume that Python internally stores integers as strings of decimal digits. That is completely false. Integers, even arbitrary precision ones (long in 3.x), are stored as bit strings. XORing two Python integers simply performs the native XOR operation on the bits as stored in memory. It loads...

c#,encryption,bitwise-operators,steganography

This does what you want. Explanations at the bottom. static Bitmap Embed(string fname, string message) { Bitmap img = new Bitmap(fname); byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(message); int nbytes = msgBytes.Length; int ibit = 7; int ibyte = 0; byte currentByte = msgBytes[ibyte]; for (int i = 0; i < img.Height; i++)...

c#,bit-manipulation,bitwise-operators,ulong

1U is not ulong. It's uint. Declare test and t as ulong and you'll get what you need: var test = 1UL; var t1 = ~test; var t = 1UL; for (int i = 0; i < 64; i++) { Console.Write((t & t1) != 0 ? 1 : 0); t...

There is no difference (or advantage, at least not on modern processors) if the bits being combined aren't overlapping (in other words, the mask on the left-hand side has zeros for all bits set by the right-hand side, and vice versa) The code shown here: // result = 0A0B0C0D 0E0F0A0B...

php,bit-manipulation,acl,bitwise-operators,user-permissions

Once you have your $perms array defined, you can actually create your own maps from it: // can comment and post but not edit or delete: $poster = $perms['can_post'] | $perms['can_comment']; // can edit and delete, but not comment or post: $janitor = $perms['can_delete'] | $perms['can_edit']; You can combine any...

python,numpy,bitwise-operators,bitarray,bitstring

As far as I can tell, the built-in Python 3 int is the only one of the options you tested that computes the & in chunks of more than one byte at a time. (I haven't fully figured out what everything in the NumPy source for this operation does, but...

Actually, -2 is ~1. Two's complement, remember? >>> bin(1) '0b1' >>> bin(~1) '-0b10' The thing is: you're not using bits, but integers. So either you want to revert to using e.g. Booleans (which read less nicely) or you want to use some expression like 0 if x else 1 to...

Your loop can more compactly be written as let byteArray = map(stride(from: 24, through: 0, by: -8)) { UInt8(truncatingBitPattern: example >> UInt32($0)) } Alternatively, create an UnsafeBufferPointer and convert that to an array: let example: UInt32 = 72 << 24 | 66 << 16 | 1 << 8 | 15...

c++,c,bit-manipulation,bitwise-operators

You can also do this with XOR and bit masking. #include <stdio.h> void f(unsigned val, unsigned ary[3]) { ary[0] = val; ary[1] = (ary[0] ^ 1) & 1; ary[2] = (ary[0] ^ 2) & 2; } int main() { unsigned ary[3] = {0}; f(0, ary); printf("f(0) = %d %d %d\n",...

c,bit-manipulation,bitwise-operators

You're toggling the even bits and then compare the result with 0. However even when all the even bits are 1s, the odd bits are still unchanged, so whenever an odd bit is 1, your function will return 0. It only works correctly when all the odd bits are 0s...

math,go,operators,bitwise-operators

Bitwise operators come into play when you have to work with byte- or bit-level data. Here I list a handful of examples using bit operations with code samples (in no particular order): 1. They are common and part of many algorithms in cryptography and hash functions (e.g. MD5). 2. They...

In C it will be : unsigned char byte = 0x19; unsigned char bit_set = (0x01 & byte) ^ byte; unsigned char bit_toggle = ((byte ^ 0x10) & 0x10); byte = ( byte & 0xEF ) | bit_toggle ; Now translate to assembly....

bit-manipulation,verilog,bitwise-operators,bitwise-and

Most likely a bug with ModelSim. Reproducible on EDAplayground with ModelSim10.1d. Works fine with Riviera2014 (After localizing l inside the generate loop). I'm guessing that and_result[m] is somehow in the @(*) sensitivity list, which it shouldn't be. l needs to be localized or it will be accessed in parallel with...

Just shift each one into place, and OR them together: #include <stdint.h> int main(void) { uint8_t a = 0xAB, b = 0xCD, c = 0xEF; /* * 'a' must be first cast to uint32_t because of the implicit conversion * to int, which is only guaranteed to be at least...

objective-c,c,hex,bitwise-operators

Try the following: /* packed starts at 0 */ uint8_t packed = 0; /* one bit of the flag is kept and shifted to the last position */ packed |= ((globalColorTableFlag & 0x1) << 7); /* three bits of the resolution are kept and shifted to the fifth position */...

javascript,if-statement,bitwise-operators

If they're just returning boolean true/false operations, then it'll work as expected: if (true & false & true) would simply be "false". But if they're turning (say) integers, then you're going to get unexpected results: if (10 & 100 & 32) would be if (b1010 & b1100100 & b100000) and...

If by 'not' you mean send 1 to 0 and 0 to 1. you can use the XOR operator ^ to do that. If character is called c, you can write c = c ^ 1;.

c++,binary,bit-manipulation,bitwise-operators,uint32

To generate a mask of n bits (where n < 32): uint32_t mask = (1U << n) - 1U; Explanation: consider the example where n = 5: 1U << n = 1U << 5 = 0000 0000 0000 0000 0000 0000 0010 0000 = 0x20 then we subtract 1 and...

sql-server,ssis,bitwise-operators,bitwise-or

I don't have a whole lot of experience with that flavor of SQL, but a bitwise OR is not the same thing as an OR clause in the WHERE statement. The bitwise OR will OR each bit of the integer together to produce a new integer For example, the numbers...

ruby,bit-manipulation,bitwise-operators

You have Integer#to_s(base) and String.to_i(base) available to you. a_and_b = (a.to_i(2) & b.to_i(2)).to_s(2) a_or_b = (a.to_i(2) | b.to_i(2)).to_s(2)...

c++,bit-manipulation,bitwise-operators

Like this, perhaps: bool result = x == y; unsigned int z = -result; ...