bit-manipulation

64 bit negative integers in two's complement form

Hi. I was reading the source of GNU PDF library, particularly their implementation of 64 bit integers. They have defined 64 bit integers as structs of two 32 bit integers - the higher order int is signed and lower order int is unsigned. Here's the relevant code from the header file: /*Definition of internal structure of the pdf_i64_t ...

how to work with strings and integers as bit strings in python?

Hello! I'm developing a Genetic Algorithm in python were chromosomes are composed of strings and integers. To apply the genetic operations, I want to convert these groups of integers and strings into bit strings. For example, if one chromosome is: ["Hello", 4, "anotherString"] I'd like it to become something like: 010010010010100101...

Compare bit sets efficiently

I want to store permissions in a bit set to compare them efficiently in Java. A 1 would mean permission granted and 0 permission denied. If the authorization is performed the required permissions would be compared to the permission set. My idea is to use AND and compare the result with the requested permissions. Example: 0010 110...

Extracting 'parts' of a hexadecimal number

I want to write a function getColor() that allows me to extract parts of a hexadecimal number entered as a long The details are as follows: //prototype and declarations enum Color { Red, Blue, Green }; int getColor(const long hexvalue, enum Color); //definition (pseudocode) int getColor(const long hexvalue, enum Color) { switch (C...

64-bit unsigned to 32-bit signed

Hi. I need to convert Java long datatype (64-bit) data into legacy c++ app unsigned int (32-bit) datatype. No worries about the data loss as the data is Linux timestamp, which would take aeons to hit unsigned int limit. Any idea what transformation to apply to these numbers? Thanks in advance! P.S. - data-types example: Java - 1266...

Why does bitwise AND of two short values result in an int value in Java?

short permissions = 0755; short requested = 0700; short result = permissions & requested; I get a compiler error: error possible loss of precision found : int required: short If I'm not totally wrong the result of binary AND is as long as the longest operand. Why is the result an integer? Would there be a performance hit, if I w...

How do I divide a string of 64bits into four 16bits in Java?

How can I split the 64 bit input into 16 bits? ...

Approved syntax for raw pointer manipulation

I am making a memory block copy routine and need to deal with blocks of raw memory in efficient chunks. My question is not about the specialized copy routine I'm making, but in how to correctly examine raw pointer alignment in C. I have a raw pointer of memory, let's say it's already cast as a non-null char *. In my architecture, I can ...

Bitstream to Float Type Coercion

I'm having trouble getting the following code to work correctly. Using an online IEEE-754 converter, I wrote out (by hand) to the testData.txt file that is read with the bit string that should signify the floating point number 75.5; the actual cout.write does show that the bit string is as I expect as well. However, when I try to coerc...

VB6 IP4 - Calculate Net Mask (Long) from Number of Bits

Given input of 0 to 32, representing the number of one-bits in an IP4 network mask (corresponding to a CIDR block size as in /19), what's An elegant way to turn that into a four-byte long net mask A fast way way to turn that into a four-byte long net mask Prototype: Function NetMaskFromBitCount(BitCount As Long) As Long 'Logic he...

Replace branch statements by bit-shifting operations

Hello, I'm writing an image binarization algorithm which simply converts each pixel's luminance value (grayscale image) to a black or white. Currently the algorithm for binarizing each pixel is roughly this if( grayscale[x] < thresholdValue) { bitonal[x] = 1; } (this is actually a simplification of the ACTUAL algorithm because the bit...

How to convert an int to a little endian byte array?

Hello, I have this function in C# to convert a little endian byte array to an integer number: int LE2INT(byte[] data) { return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0]; } Now I want to convert it back to little endian.. Something like byte[] INT2LE(int data) { // ... } Any idea? Thanks. ...

Variable length integer encoding

I am attempting to reverse engineer an LZ1/LZ77 decompression algorithm. The length of an area of the decode buffer/window to be output is encoded in the file as a variable length integer. I've read as much as I can about variable length integer encoding and the method being used in this case does not appear to be like any others I ha...

C: Unpacking a byte

I asked the question here and now i would like to reverse this procedure I have an unsigned short that represents a bit pattern, I would like an array of bool[16] that has true or false set for the corresponding bit in the unsigned short. so far i have where binary 2 is an unsigned short, that has the bit pattern i wish to unpack. un...

what is c/c++ equivalent way of doing '>>>' as in java (unsigned right shift)

Hi I wanted to know how i can do '>>>' shifting of java in c/c++. thanks ...

how to calculate bitwise OR using AND, XOR and shift?

Hi The question seems pretty well formulated I have a virtual machine which implements only AND, XOR, SHL and SHR, yet I have to do a "OR 0x01" operation. ...

How to set bit values which are not divided on byte boundary

Hi I have a header field of 2 bytes where first four bits are packet type and other 12 bits are for length. but i don't know how to store this values and retrieve them. I am using ms c++ for client and java for server. It is the client which has to set this values and server has to retrieve it. Thanks ...

Learning C bit manipulation

I didn't know any better name for a title of the question. For ex: on one of my previous questions one answered ((a)-(b))&0x80000000) >> 31 - this is kind of a too advanced for me and i can't really get what it means. I am not looking just for an answer of that, but i need someone to tell me some books/sites/whatever where i can learn ...

BitSet to and from integer/long

If I have an integer that I'd like to perform bit manipulation on, how can I load it into a java.util.BitSet? How can I convert it back to an int or long? I'm not so concerned about the size of the BitSet -- it will always be 32 or 64 bits long. I'd just like to use the set(), clear(), nextSetBit(), and nextClearBit() methods rather t...

Iterating bits of a char

Assuming I have char "C" whose ascii code is 0110 0111. How can I iterate over its bits? I would like to build a vector from these 1's and 0's.... ...