python,bit-manipulation,negative-number
I'm working on a Python Application that reads from a binary file, pulls a byte that then references a list. This is essentially what I'm doing. list = [x for x in range(0, 340)] index = struct.unpack('<b', file.read(1)) print(list[index]) The problem is that the file I'm reading from, the 8th...

java,binary,bit-manipulation,twos-complement,sign-extension
I am trying to perform a bitwise or on a byte value I have in Java. For example, I am running: byte b = (byte)0b11111111; int result = 0 | b; My expected result for this would be 0b00000000 00000000 00000000 11111111, or 255. However, I am receiving -1, or...

c,binary,double,bit-manipulation,arc4random
I'm trying to create a double by appending the binary representations of two ints. This is what I have now: int i = arc4random(); *(&i+1) = arc4random(); double d = *(double*)&i; I hoped the double pointer would also use the value in the &i+1 address, and this is indeed the...

c,integer,compare,bit-manipulation,string-comparison
I have small vectors. Each of them is made of 10 integers which are between 0 and 15. This means that every element in a vector can be written using 4 bits. Hence I can concatenate my vector elements and store the whole vector in a single long type (in...

go,redis,bit-manipulation,bit
Using redis#Setbit to set bit in a key like: redis.Do("SETBIT", "mykey", 1, 1). When I read it using redis#Get like redis.Do("GET", "mykey"), I get a bit string. How do I unpack the string so I can get a slice of bools in Go? In Ruby, you use String#unpack like "@".unpack...

c++,bit-manipulation,pseudocode
I was looking though some pseudocode for some code and I came across the phrase lowest 32 bits of After a very long time of searching through website I came across the answer: What we want to do is AND operation on 64 bit number and 0xffffffff to get lowest...

c++11,int,bit-manipulation,bit
I have a number of type int. It lies within [0,255]. That is, includes 8 bits. I need to check often say: 2(int) = 00000010(Binary) 1. The bit 6 and bit 7 must be equal to 0 and 1 respectively. And I check it like this: if ((!(informationOctet_ & (1...

c#,.net,bit-manipulation,bit-shift
I want to create a bitmask and the bitmask offset by offset and length. For example: offset = 8 and length = 4 the bitmask should be: 0xF00 = 3840 in decimal(1111.0000.0000) and the bitoffset should be: 0x100 = 256 in decimal I've tried the following, but that's wrong: ulong...

c,bit-manipulation,bitwise-operators,firmware
data |= (1 << 3) sets bit (3) without disrupting other bits. data &= ~(1 << 4) resets bit (4) without disrupting other bits. How can I accomplish both tasks in a single instruction? (As this is really only for readability, I plan on #defineing this in a cute way...

java,algorithm,bit-manipulation,xor,bitwise-xor
I came across this problem wherein all of the program was given barring only a piece of logic. What could be filled in the following problem's missing lines? Problem Statement Jack and Daniel are friends. They want to encrypt their conversation so that they can save themselves from interception by...

bit-manipulation
There is not my front-end code that is converting X from int to short and save it in a binary stream write(x & 0xFF) write((x & 0xFFFF) >> 8 ) (I am C++ back-end dev and not sure what the language this is.. looks like JS but there is no...

c++,bit-manipulation
I am writing a function to print bits but i ran into a problem. The two for loops seem identical to me but for some reason the one that is commented out does not print the right bits. int main() { unsigned int bits = 0x570FA1; unsigned int mask =...

c++,gcc,bit-manipulation,sse
How can I count the number of leading zeros in a 128-bit integer (uint128_t) efficiently? I know GCC's built-in functions: __builtin_clz, __builtin_clzl, __builtin_clzll __builtin_ffs, __builtin_ffsl, __builtin_ffsll However, these functions only work with 32- and 64-bit integers. I also found some SSE instructions: __lzcnt16, __lzcnt, __lzcnt64 As you may guess, these...

objective-c,c,bit-manipulation,bitwise-operators
I've been experimenting with changing values for some of the bits for field packing a byte, based on my last question: Field packing to form a single byte However, I'm getting unexpected results based on the values. The top code sample gives me an expected output of 0x91, however if...

bit-manipulation
I've been looking at some of these books with fun interview problems. One has a question where one is supposed to write code to flip two bits in a 64-bit integer given the indices of the two bits. After playing around with this for a while I came up with...

java,bit-manipulation
How do I shift a byte array n positions to the right? For instance shifting a 16 byte array right 29 positions? I read somewhere it can be done using a long? Would using a long work like this: Long k1 = byte array from 0 to 7 Long k2...

c++,c,bit-manipulation,bitwise-operators
So basically int num = rand(2); //random number from 0-2 int otherNum, otherOtherNum; otherNum = implement this otherOtherNum = implement this For example, if num is 2, otherNum and otherOtherNum must be set to 0 and 1 (or 1 and 0). How would you implement this? Assume you can't use...

python,bit-manipulation,music
In just intonation theory, a type of music theory, intervals between notes are expressed with rational numbers. Doubling a frequency with a 2:1 ratio brings it up an octave and a 1:1 ratio is no change; a unison. So if I have an interval n that is larger than an...

python,c++,algorithm,bit-manipulation
I was trying to solve an old question: Write a function that add two [integer] numbers A and B. You should not use + or any arithmetic operators. The best solution is like this, quote from here: For a + b in any base, we can treat the plus as...

bit-manipulation,bit-shift
I have this: int line = 0; line |= (1 << 0); line |= (1 << 1); line |= 0000000000; I was expecting an output of 000000011. But the output is 11. Why is my result truncated? I am using Java. So System.out.print();...

c,bit-manipulation,mask
I'm working on a function that returns 1 when x can be represented as an n-bit, 2’s complement number and 0 if it can't. Right now my code works for some examples like (5,3),(-4,3). But I cant get it to work for instances where n is bigger than x like...

c#,bit-manipulation,byte
This question already has an answer here: C#. Logical riddle with bit operations. Only one bit is set? 3 answers I'm receiving a 3-byte message back from a serial device when I send it a query in my C# application. The entire message should have at most one bit...

c#,.net,bit-manipulation,bit
Good morning, don't know if this question is hitting the right StackExchange network. If not please forgive me. I got an decimal/integer identifier value which i need to split into three separate values. Lets assume we have program ABC. Program ABC is generating the decimal/integer identifier value using this formula:...

c#,bit-manipulation,bitwise-and
I've got some code in another language which uses an enum and a list of values from the enum and then uses bitwise to get an integer for the list of values from the enum. HELLO = 1 WORLD = 2 AND = 4 SO = 8 ON = 16...

c,hex,bit-manipulation
I am trying to figure out how to find all set bits in a hex number and then shift those bits to the left most positions using a 32 bit notation. My program reads in args from the command prompt and then calls the appropriate function and passes either the...

algorithm,bit-manipulation
Given a 64 bit number, I need to extract every other bit from it, and convert it into a number: decimal: 357 binary: 0000 0001 0110 0101 odd bits: 0 0 0 1 1 0 1 1 decimal: 27 Any idea of a good algorithmic way to do it? And...

ruby,integer,bit-manipulation,byte
We are having some small problems when converting data from hex to integer in ruby... We get a hardware signal, which is integer data -> converted to high and low byte, bitwise reversed -> string. Here is a example: Something between 0 and 75 => will be converted to "0x3938"......

c,bit-manipulation,bitwise-operators,bit-shift
Is this correct way to check if bits are set in some variable and store them in another variable? Just ignore variable names, image[] and mask variables are 1 byte and first_hline_first_row and second_hline_first_row are 2 byte. for(i=0; i<16; i++){ if(image[i] & mask1) first_hline_first_row |= (1<<i); else first_hline_first_row &=~ (0<<i);...

python,bit-manipulation
>>> result = 0 >>> x = -2 >>> >>> for i in range(32): ... c = 1 << i ... if c & x: result |= c ... >>> print(result) 4294967294 As you can see, the expected result should be -2, however the program prints 4294967294, which is the...

javascript,node.js,bit-manipulation,tostring,parseint
This question is similar to this other question; however, I'd like to understand why this is working as it is. The following code: console.log((parseInt('0xdeadbeef', 16) & parseInt('0x000000ff', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) & parseInt('0x0000ff00', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) & parseInt('0x00ff0000', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) & parseInt('0xff000000', 16)).toString(16)); console.log((parseInt('0xdeadbeef', 16) &...

javascript,bit-manipulation
I have an array of ints var ints = [R,B,G,A] and I want to use shifting to get a 32-bit representation var thirtyTwo = AGBR so for example, [255.0, 0.0, 0.0, 255.0] => 0xFF0000FF => 4278190335 I'm attempting to do this with a loop and bitshift: function cArrayToABGR(va) { var...

c++,bit-manipulation,bit,bitwise-operators
I have the following code int n = 50; while(n) { //1 if(n & 1) cout << "1" << endl; //2 //right shift the number so n will become 0 eventually and the loop will terminate n >>= 1; //3 } When we use bitwise and 1 (& 1) with...

c,bit-manipulation,toggle
I am working on bitwise manipulation (in C) and I wanted to know how to check if a bit has toggled between a previous value and the new value. Example : oldValue = 0x0FF //0000 1111 1111 in binary newValue = 0x100 //0001 0000 0000 in binary In this example...

assembly,bit-manipulation,mainframe
In IBM OS/390 assembly, I am trying to do the following: I have a set of bits that all end in 2 zeroes: 00xxxxxx 00yyyyyy 00zzzzzz I want to compress them into the following format: xxxxxxyy yyyyzzzz zz... I figure that the order will something like ICM R7,B'1111',FIRSTBUF LOAD 4...

algorithm,for-loop,bit-manipulation,modulus
This is an incredibly specific question, as i simply need to know if this pattern has a name and if there is a more effective way of doing it. (disregard the obvious declaration in the for loop) for (int i = 0; i < 8; i++) { int mod =...

javascript,performance,bit-manipulation
According to w3schools Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc. JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard. So my question comes: if we want to perform a bitwise...

c++,c,bit-manipulation
Using bitwise operator how can I test if the n least significant bits of an integer are either all sets or all not sets. For example if n = 3 I only care about the 3 least significant bits the test should return true for 0 and 7 and false...

c,bit-manipulation,machine-code
I'm writing code to translate machine code. readFiveBytes is inside of a loop and supposed to take one byte at a time and place each byte into its correct place and return an address in the end. void readFiveBytes(int c) { if (counter != 0) { currentIns = currentIns |...

java,bit-manipulation,unary-operator
Most are probably familiar with this problem but for those who are not: Given 32-bit unsigned integers flip their bits and print the resulting integers. I was hoping that someone can give me clueson how to solve this in Java. Now I would normally provide some code i have tried...

java,algorithm,integer,bit-manipulation
This is from a hacker rank practice problem(not a competition) https://www.hackerrank.com/challenges/flipping-bits. Just doing this for practice. The problem just asks you to take a set number for 32 bit integers and for each one, invert all the bits inside that integer and print out the result Here's my code so...

c,syntax,bit-manipulation,bitwise-operators
I wanted to try to get only the four bits from the right in a byte by using only bit shift operations but it sometimes worked and sometimes not, but I don't understand why. Here's an example: unsigned char b = foo; //say foo is 1000 1010 unsigned char temp=0u;...

c,bit-manipulation,micro-optimization
As part of my CS classes I've recently completed the pretty popular "Data Lab" assignments. In these assignments you are supposed to implement simple binary operations in C with as few operations as possible. For those who are not familiar with the "Data Lab" a quick overview about the rules:...

swift,int,boolean,type-conversion,bit-manipulation
In ObjC, a bool's bit pattern could be retrieved by casting it to a UInt8. e.g. true => 0x01 false => 0x00 This bit pattern could then be used in further bit manipulation operations. Now I want to do the same in Swift. What I got working so far is...

c,x86,integer,bit-manipulation,sse
I have created a function which does 64-bit * 64-bit to 128-bit using SIMD. Currently I have implemented it using SSE2 (acutally SSE4.1). This means it does two 64b*64b to 128b products at the same time. The same idea could be extended to AVX2 or AVX512 giving four or eight...

c++,bit-manipulation,operators
I am trying to clearly understand the behaviour of the shift operators (especially for the boundary cases), so I devised a little test written in C++. int a = odd_value; //321 in my case but it should not matter (the last bit to be 1) print(0, a); a = a...

c,bit-manipulation,byte,bit
I could use this: unsigned long alpha = 140 | 130 << 8 | 255 << 16; to set 140 to the first byte of alpha, 130 to the second and 255 as 3rd. How do I do the opposite (i.e checking a specific byte of alpha) ? alpha &...

c#,.net,binary,bit-manipulation,endianness
I need to read binary files containing millions of Int16 stored as big endian. My first method was to use BitConverter and Array.Reverse() but that appears too slow for my purpose. Is there a way to do it with bitwise arithmetic instead ?...

vb.net,bit-manipulation,xor
I'm trying to get an application to XOR two hexadecimal values and output the result as a decimal value. I have searched the Internet for an answer, but every result simply gives a true or false result. The figures I am using as an example are Value 1 = 0F8F...

c,bit-manipulation,standards,bit
As per the C standard the value representation of a integer type is implementation defined. So 5 might not be represented as 00000000000000000000000000000101 or -1 as 11111111111111111111111111111111 as we usually assume in a 32-bit 2's complement. So even though the operators ~, << and >> are well defined, the bit...

c,hash,bit-manipulation
I'm trying to write hashset in C, and I've found one hash function, that hashes according to bits in data. I have the following structure: struct triple { int a; int b; int c; }; The question is - how to get bit representation from object of type struct triple?...

c++,bit-manipulation
This question already has an answer here: What are bitwise shift (bit-shift) operators and how do they work? 5 answers given int a = 1; (00000000000000000000000000000001), what I did is just a=(a<<31)>>31; I assume a should still be 1 after this statement (nothing changed I think). However, it turns...

c++,c,memory,bit-manipulation,information-theory
I have the following problem I am unable to solve gracefully. I have a data type that can take 3 possible values (0,1,2). I have an array of 20 element of this data type. As I want to encode the information on the least amount of memory, I did the...

java,bit-manipulation,bit
The Problem Hi there, i recently moved to using a binary representation for a domain in my program, this enhanced the speed of calculations with that domain, but i now need a method of generating ints based on an input number of bits needed. My application is in Java so...

java,bit-manipulation
I've come across some code which has the bit masks 0xff and 0xff00 or in 16 bit binary form 00000000 11111111 and 11111111 00000000. /** * Function to check if the given string is in GZIP Format. * * @param inString String to check. * @return True if GZIP Compressed...

java,bit-manipulation,nio
I've been following this tutorial, it's quite helpful and most things are making sense. If you look on page 34 you'll see this snippet of code: } else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) { // Read the data SocketChannel sc = (SocketChannel)key.channel(); // ... } What exactly is going...

c,bit-manipulation,bit
You are given a getTemps() function returns an integer composed of: the daily high temperature in bits 20-29, the daily low temperature in bits 10-19, and the current temperature in bits 0-9, all as 2's complement 10-bit integers. Write a C program which extracts the high, low, and current temperature...

python,c++,algorithm,data-structures,bit-manipulation
I have an array containing N elements and I need to find the distance between the index of equal elements in a subarray; which we will get in form of query (L R) where L is starting index of subarray and R in ending index. Total No. of array elements...

javascript,syntax,bit-manipulation
This question already has an answer here: What does the “|” (single pipe) do in JavaScript? 3 answers I've seen a single | being used in code somewhere but I have no idea what it means. I know that || is 'or'. I typed it in the console and...

matlab,bit-manipulation,octave
I'm doing analysis on binary data. Suppose I have two uint8 data values: a = uint8(0xAB); b = uint8(0xCD); I want to take the lower two bits from a, and whole content from b, to make a 10 bit value. In C-style, it should be like: (a[2:1] << 8) |...

python,parsing,python-3.x,bit-manipulation,bin
If I have 3 bytes b'\x00\x0c\x00', which can be represented with the bits 00000000 00001100 00000000, how do I then parse the 11th and 12th bit 11 most efficiently? Here positions: ** 00000000 11111110 22222111 tens 87654321 65432109 43210987 ones |||||||| |||||||| |||||||| 00000000 00001100 00000000 ** I have the...

python,python-3.x,bit-manipulation
Edit: Problem solved, advert your eyes from the below code though, haha, I had some things conceptually wrong from the beginning. I've been trying for longer than I care to admit to count the non-zero bits in a wide variety of 32-bit integers, but always seem a bit off. So...

php,bit-manipulation,32-bit
The code: echo (5243960811416 & 4040906070209050); Try on http://phptester.net/ and the result (right) will be 20407554584 but on my web hosting it gives -1067281896. There's any workaround to have 20407554584? It's a 32bit limit? Thanks UPDATE /w SOLUTION...

c,bit-manipulation
In some old C/C++ graphics related code, that I have to port to Java and JavaScript I found this: b = (b+1 + (b >> 8)) >> 8; // very fast Where b is short int for blue, and same code is seen for r and b (red & blue)....

arrays,set,bit-manipulation,bitwise-operators
Can anyone give a hint how to approach this problem? Given an array A. Is there any subset of array A in which if we do AND of all elements of that subset then output should be in power of two. I've thought of generating a power-set and solving but...

operators,bit-manipulation
I am learning Bitwise Operators and for that I pulled out an article on tutsplus. Well, it is written quite nicely. I could understand the & and | operator but the ~ is messing things up. For example, as stated there in the article: In fact, just as ! flips...

java,algorithm,bit-manipulation
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at j) Example Given N=(10000000000)2, M=(10101)2,...

c++,bit-manipulation,bit-shift,bitwise-or,bitboard
I'm trying to deal with bitboards, which requires me to set a particular bit in a 64-bit unsigned integer. To set bit i, I perform a bitwise OR with the bitboard in question, with a left shifted number. #include <stdint.h> uint64_t kings = 0ULL; // Also tried unsigned long long...

c++,c,int,byte,bit-manipulation
Excuse me for my english. I have a number of int values stored in it from 0 to 255. To find out what lies in 7 bit number, I translate the numbers into a binary system, then in the line and check the line: if (informationOctet_.substr(6, 1) == "0") {...

c,bit-manipulation,bit
I am trying to come up with a function int rotateRight(int x, int n) that Rotate x to the right by n. For example,rotateRight(0x87654321,4) = 0x76543218. This is what I have so far int rotateRight(int x, int n) { int mask = (((1 << n)-1)<<(32-n)); int reserve = (int)((unsigned) (x&mask)...

c,bit-manipulation,bit
given a number (int a = 0XABCDE98) i am trying to set the D bit to 6. ie after the Bit manipulation the number should be (0XABC6E98). I have written the small C program to do the bit manipulation, but somehow i am not able to see the correct bit...

java,arrays,bit-manipulation,xor
You are given an array A1,A2...AN. You have to tell how many pairs (i, j) exist such that 1 ≤ i < j ≤ N and Ai XOR Aj is odd. Input and Output First line T, the number of testcases. Each testcase: first line N, followed by N...

java,android,bit-manipulation,bitwise-operators
I am getting mad, i need to create a token using this operator PHP a = "hello"; b = "world"; token = a | b; well i need create the same in java [android] but i ever get error. I alredy try to: - cast the 2 strings to long...

c,bit-manipulation,bit-shift
Is it possible to shift 0s as you can shift 1s in shifting operation in C? Something like this for (i=0; i<32; i++) { if (data & 0x01) { data |= (1<<i); } else { data &=~ (0<<i); } } I'm checking if some bits are set and depending on...

php,bit-manipulation
I have an array of string bits of variable length and I need to negate them. eg. input array("1", "00101", "101", "0000") output array("0", "11010", "010", "1111") Is there a simple way how to do that?...

c++,return,bit-manipulation,ampersand
I'm not entirely sure what happens in this process and what the outcome would be. I've never come across using && in a return or especially a single one in the same return statement. bool myfunction(unsigned int x) { return x && !(x & (x­ - 1)); } I'd love...