bit-manipulation

Finding the absolute value of a number in 8085 microprocessor assembly language

霸气de小男生 提交于 2020-01-24 12:11:25
问题 I have a task of finding the absolute value of any given number in 8085 assembly language. The algorithm is the following (found on the internet): mask = n >> 7 (number itself is 8 bits) (mask + n) XOR mask My question is that how would I implement this in assembly language. It seems that I should be using the "RRC" command but that performs circular shift on the number and the algorithm doesnt seem to work. Any ideas would be appreciated. Cheers. 回答1: The n>>7 in that abs algorithm is an

Obtaining max of unsigned integer with bitwise not on zero value

时光怂恿深爱的人放手 提交于 2020-01-24 12:01:24
问题 I'm trying to obtain the maximum value of a certain unsigned integer type without including any headers like <limits> . So I thought I'd simply flip the bits of the unsigned integer value 0. #include <iostream> #include <limits> int main() { std::cout << (~0U) << '\n'; // #1 std::cout << (std::numeric_limits< unsigned >::max()) << '\n'; // #2 return 0; } I'm not very experienced on the subtle differences between these. Which is why I'm asking if some unexpected behavior or some platform

Obtaining max of unsigned integer with bitwise not on zero value

依然范特西╮ 提交于 2020-01-24 12:01:07
问题 I'm trying to obtain the maximum value of a certain unsigned integer type without including any headers like <limits> . So I thought I'd simply flip the bits of the unsigned integer value 0. #include <iostream> #include <limits> int main() { std::cout << (~0U) << '\n'; // #1 std::cout << (std::numeric_limits< unsigned >::max()) << '\n'; // #2 return 0; } I'm not very experienced on the subtle differences between these. Which is why I'm asking if some unexpected behavior or some platform

How to work with bitfields longer than 64 bits?

[亡魂溺海] 提交于 2020-01-23 05:49:06
问题 Question says it all. If I have this for a 96-bit field: uint32_t flags[3]; //(thanks @jalf!) How do I best go about accessing this, given that my subfields therein may lie over the 32-bit boundaries (eg. a field that runs from bit 29 to 35)? I need my accesses to be as fast as possible, so I'd rather not iterate over them as 32-bit elements of an array. 回答1: [This answer is valid for C (and by extension, for C++ as well).] The platform-independent way is to apply bit-masks and bit-shifts as

One function with different arguments to push certain bits of an input integer to the left

旧时模样 提交于 2020-01-23 03:59:08
问题 This question is related to this. Let x be an 8 bit integer. I will number bits from left to right because that's how we read. If I want to get the third and fifth bits and put them in the first and second bits, with everything else as zero, I can have f(x) = (5*x) & 0b11000000 . More concisely: 00a0b000 -> ab000000 | f_0b00101000(x) = (5*x) & 0b11000000 However if I want the fifth, sixth and eighth bits to be in the first three bits, f(x) is different: 000ab0cd -> abcd0000 | f_0b00011011(x)

Unpacking a bitfield (Inverse of movmskb)

谁说胖子不能爱 提交于 2020-01-23 03:36:09
问题 MOVMSKB does a really nice job of packing byte fields into bits. However I want to do the reverse. I have a bit field of 16 bits that I want to put into a XMM register. 1 byte field per bit. Preferably a set bit should set the MSB (0x80) of each byte field, but I can live with a set bit resulting in a 0xFF result in the byte field. I've seen the following option on https://software.intel.com/en-us/forums/intel-isa-extensions/topic/298374: movd mm0, eax punpcklbw mm0, mm0 pshufw mm0, mm0, 0x00

Intrinsic to count trailing zero bits in 64-bit integers?

房东的猫 提交于 2020-01-21 08:59:26
问题 this is sort of a follow up on some previous questions on bit manipulation. I modified the code from this site to enumerate strings with K of N bits set (x is the current int64_t with K bits set, and at the end of this code it is the lexicographically next integer with K bits set): int64_t b, t, c, m, r,z; b = x & -x; t = x + b; c = x^t; // was m = (c >> 2)/b per link z = __builtin_ctz(x); m = c >> 2+z; x = t|m; The modification using __builtin_ctz() works fine as long as the least

Encrypt only the content of an image file not entire file

邮差的信 提交于 2020-01-21 05:52:46
问题 I am creating an APP and need to encript only the content of an image. I need that the file was still an image after conversion, but the image showed does not show as the original. For example, I will send the image encrypted to other user and this one will be able to show and image (but not the original), but the original image was encrypted in that file. With the following algorythm I encrypted the entire file, and this cannot be opened as image due the header is encrypted as well. I am

bitwise most significant set bit

安稳与你 提交于 2020-01-20 08:07:06
问题 I want to find the most significant bit that is set to 1 . I have tried every possible way from & to ORing all of the bits from 1 to 31 and it doesn't work. Like if 1000000 I would like to have 7 . 回答1: If you insist on directly using bitwise operators, you can try something like this: private int mostSignificantBit(int myInt){ int mask = 1 << 31; for(int bitIndex = 31; bitIndex >= 0; bitIndex--){ if((myInt & mask) != 0){ return bitIndex; } mask >>>= 1; } return -1; } We initialize the mask

How do I represent binary numbers in C++ (used for Huffman encoder)?

家住魔仙堡 提交于 2020-01-17 07:50:12
问题 I am writing my own Huffman encoder, and so far I have created the Huffman tree by using a minHeap to pop off the two lowest frequency nodes and make a node that links to them and then pushing the new node back one (lather, rinse, repeat until only one node). So now I have created the tree, but I need to use this tree to assign codes to each character. My problem is I don't know how to store the binary representation of a number in C++. I remember reading that unsigned char is the standard