bit-manipulation

Updating fields of bits incorrectly

坚强是说给别人听的谎言 提交于 2019-12-02 01:35:51
I'm trying to solve a question. It says, Initialize a new variable to the value 17512807u. Assume we number the bits as usual from 0 as least significant (on the right) to 31 (most significant, on the left). Update bits 18 through 21 with the integer value 8 and bits 10 through 14 with value 17 (decimal). Print the resulting value as an eight digit hexadecimal number to show all of the digits. Here's the code I came up with: #include <stdio.h> int main(){ int value = 17512807u; int L = 21; // starting left position int R = 18; // starting right position int mask = (1 << (L - R + 1) - 1) << R;

Swapping bits in a positive 32bit integer in C#

点点圈 提交于 2019-12-02 00:52:53
So I'm trying to solve this problem: You are given random 32bit positive integer, and what you have to do is swap the values of the bits on 3rd, 4th and 5th positions with those on 24th, 25th and 26th position. Assuming that this is a problem for which you do not want an explicit solution, here is a hint: mask the bits in question using & , do a shift, and then OR then in using bitwise | . You can "cut out" bits 3, 4, and 5 using the 0x00000034 mask, and bits 24, 25, and 26 using the 0x07000000 mask. Take a look at this solution to bit reversing problem for an inspiration. EDIT : (in response

Simulating Floating Point Multiplication in C using Bitwise Operators [closed]

放肆的年华 提交于 2019-12-02 00:50:00
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 10 months ago . I have to write a program that will simulate floating point multiplication. For this program, we assume that a single precision floating point number is stored in unsigned long a . I have to multiply the number stored in a by 2 using only the following operators: << >> | & ~ ^ I

How to shift >= 32 bits in uint64_t?

被刻印的时光 ゝ 提交于 2019-12-02 00:49:06
问题 The following code triggers a gcc warning (gcc 4.2.1): #include <boost/cstdint.hpp> boost::uint64_t x = 1 << 32; // warning: left shift count >= width of type Shouldn't it be fine since the type has 64 bits? 回答1: How to shift >= 32 bits in uint64_t ? If your compiler supports long long : boost::uint64_t x = 1LL << 32; Otherwise: boost::uint64_t x = boost::uint64_t(1) << 32; Shouldn't it be fine since the type has 64 bits? No. Even though x is 64 bits, 1 isn't. 1 is 32 bits. How you use a

Reading least significant bits in Python

家住魔仙堡 提交于 2019-12-02 00:28:08
问题 I am having to parse the Facility and Severity of syslog messages in Python. These values come with each message as a single integer. The severity of the event is 0-7, specified in the 3 least significant bits in the integer. What is the easiest/fastest way to evaluate these 3 bits from the number? The code I have right now just does a 3 bit right shift, than multiplies that number times 8, and subtracts the result from the original. FAC = (int(PRI) >> 3) SEV = PRI - (FAC * 8) There must be a

Bitstream of variable-length Huffman codes - How to write to file?

天大地大妈咪最大 提交于 2019-12-02 00:16:02
I'm working on a Huffman coding/decoding project in C and have a good understanding of how the algorithm should store information about the Huffman tree, re-build the tree during decoding, and decompress to the original input file using variable-length codes. When writing to my compressed file, I will output a table of 256 4-byte integers containing unique frequencies, and I know I will also have to figure out a way to handle EOF - worrying about that later. My question is how should I complete the necessary bit-wise operations to write a stream of variable-length codes to a series of 1-byte

Unexepected behavior from multiple bitwise shifts on the same line [duplicate]

為{幸葍}努か 提交于 2019-12-02 00:03:51
This question already has an answer here: Why does combining two shifts of a uint8_t produce a different result? 2 answers I get a different result depending on if I combine multiple bitwise shifts on one line or put them on separate lines. unsigned char a = 73; a = (a << 6) >> 2; printf("%d\n", a); prints 144 when I expect 16 unsigned char a = 73; a = a << 6; a = a >> 2; printf("%d\n", a); prints the expected result of 16 when I shift left 6, then right 1 on one line, it prints the expected result of 32, so it looks like the 2nd shift right is shifting in a 1 instead of a 0, but why? Also a =

Bitwise concatenation in C

三世轮回 提交于 2019-12-01 23:54:36
I'm trying to concatenate two binary numbers in C. So if I have 1010 and 0011 I want my result to be 10100011 . I wrote a short routine that I thought would do the job: #include <stdio.h> int main(void) { int first = 1010; int second = 0011; int result = (first << 4) | second; printf("%d", result); return 0; } I understand that the printed number of course is going to be in decimal, but I figured that after my bitwise operations I'd be getting the decimal equivalent of 10100011 , or 163. However, my result is printed as 16169. So I guess my question is...what part of this am I not

efficient way to divide a very large number stored in 2 registers by a constant

只愿长相守 提交于 2019-12-01 23:01:38
Let's say I want to calculate the following: A/Z Where A is of length 128 bit and Z is 64 bit long. A is stored in 2 64 bit registers since the registers of the system can store up to 64 bits. What would be an efficient way to calculate the result? P.S: I've solved similar multiplication problems by using CSD representations. However, this would require calculating 1/Z first. The right way to solve such a problem, is by returning to the basics: divide the most significant register by the denominator calculate the quotient Q and the rest R define a new temporary register preferrably with the

How to shift >= 32 bits in uint64_t?

回眸只為那壹抹淺笑 提交于 2019-12-01 22:31:11
The following code triggers a gcc warning (gcc 4.2.1): #include <boost/cstdint.hpp> boost::uint64_t x = 1 << 32; // warning: left shift count >= width of type Shouldn't it be fine since the type has 64 bits? How to shift >= 32 bits in uint64_t ? If your compiler supports long long : boost::uint64_t x = 1LL << 32; Otherwise: boost::uint64_t x = boost::uint64_t(1) << 32; Shouldn't it be fine since the type has 64 bits? No. Even though x is 64 bits, 1 isn't. 1 is 32 bits. How you use a result has no effect on how that result is generated. 来源: https://stackoverflow.com/questions/14846566/how-to