bit-manipulation

How to use bitshifting in Java

百般思念 提交于 2019-12-02 10:45:12
I am trying to construct an IP header. An IP header has the following fields: Version, IHL, DSCP etc. I would like to populate a Byte Array such that I can store the information in bytes. Where I get confused however is that the Version field is only 4 bits wide. IHL is also only 4 bits wide. How do I fit the values of both of those fields to be represented as a byte? Do I need to do bitshifting? E.g. Version = 4, IHL = 5. I would need to create a byte that would equal 0100 0101 = 45h or 69 decimal. (byte) (4 << 4) | 5 This shifts the value 4 to the left, then sets lower 4 bits to the value 5.

c++ portable conversion of long to double

送分小仙女□ 提交于 2019-12-02 10:16:08
问题 I need to accurately convert a long representing bits to a double and my soluton shall be portable to different architectures (being able to be standard across compilers as g++ and clang++ woulf be great too). I'm writing a fast approximation for computing the exp function as suggested in this question answers. double fast_exp(double val) { double result = 0; unsigned long temp = (unsigned long)(1512775 * val + 1072632447); /* to convert from long bits to double, but must check if they have

Why does 3,758,096,384 << 1 give 768

天涯浪子 提交于 2019-12-02 10:11:17
问题 After reading the great answer for Absolute Beginner's Guide to Bit Shifting? I tested the claim (sic): 3,758,096,384 << 1 from Chrome console: 3,758,096,384 << 1 > 768 3,758,096,384 << 2 > 1536 3758096384 << 1 > -1073741824 回答1: That's the comma operator at work. It's actually 384 << 1 . (The comma operator evaluates its left hand side, then evaluates its right hand side, and returns the right hand side.) 回答2: It returns 768 because you're incorrectly using the comma operator. 3,758,096,384

Algorith problem Decode Hex to set output

落爺英雄遲暮 提交于 2019-12-02 09:56:16
I got an algorithm that I need to solve. Unfortunately, I can't even find a clue about a solution. Please help me to understand how to solve this problem. The problem An imaginary pen input device sends hex data to the device to display a line with color. Examples Draw simple Green Line Set color to green, draw a line from (0,0) to (4000, 4000). Filled circle in this diagram indicates a pen down position, empty circle indicates pen up position. Input Data: F0A04000417F4000417FC040004000804001C05F205F20804000 Output: CLR; CO 0 255 0 255; MV (0, 0); PEN DOWN; MV (4000, 4000); PEN UP; I got the

Bitwise shift left and right in the same statement

冷暖自知 提交于 2019-12-02 09:02:14
Is char c2=i1<<8>>24; valid C syntax? (Where i1 is and unsigned integer) Additionally, will it yield the result of shifting i1 8 bits left and 24 bits right respectively? I am unpacking a char previously stored in i1 , along with three other chars. Code below: unsigned char b3 = 202; unsigned char b2 = 254; unsigned char b1 = 186; unsigned char b0 = 190; ... unsigned int i1=202; i1=i1<<8; i1=i1+254; i1=i1<<8; i1=i1+186; i1=i1<<8; i1=i1+190; ... char c1=i1>>24; char c2=i1<<8>>24; The syntax is fine (although hard to read) and it will be parsed as c2 = (i1 << 8) >> 24 . So it will left shift i1

Find if x is bigger than y using bitwise operator in C [closed]

£可爱£侵袭症+ 提交于 2019-12-02 09:02:09
If x > y, then this function will return 1, other wise return 0. so far i have int isitGreater(int x, int y) { return (((y+((~x)+1)) >> 31) & 1); but it's not working. Allowed ops: Legal ops: ! ~ & ^ | + << >> I'm sure I have the logic right, if X - Y and I get a negative number, that means y > x , so therefore the 32nd bit is a 1, so I shift that bit to the right 31 times and then "and" it with "1". edit: this does not work if x is negative, due to overflow. how can i fix this overflow problem without using conditional statements? Your code works fine for me. Please submit a valid question.

Number of pairs with constant difference and bitwise AND zero

走远了吗. 提交于 2019-12-02 08:34:27
How to find the number of pairs whose difference is a given constant and their bitwise AND is zero? Basically, all (x,y) such that x-y = k; where k is a given constant and x&y = 0; An interesting problem. Let k n-1 ...k 1 k 0 be the the binary representation of k . Let l be the index of the smallest i such that k i =1 We can remark that a potential pair of solutions x and y must have all their bits i , i<l at zero. Otherwise the only way to have a difference x-y with its i th bit unset would be to have x i =y i =1 and x&y will not have its i th bit unset. Now we arrive at the first bit at one

Bitwise NOT operator returning unexpected and negative value? [duplicate]

强颜欢笑 提交于 2019-12-02 08:32:06
This question already has an answer here: Why is the output -33 for this code snippet 3 answers I'm trying to get the value of an integer using Bitwise NOT, but i'm not getting what i expected. #include <stdio.h> int main(){ int i = 16; int j = ~i; printf("%d", j); return 0; } Isn't 16 supposed to be: 00000000000000000000000000010000 So ~16 is supposed to be: 11111111111111111111111111101111 Why i'm not getting what i expected and why the result is negative? This is what i'm trying to do: I have a number for exemple 27 which is: 00000000000000000000000000011011 And want to check every bit if

Extracting bits with bitwise operators [closed]

萝らか妹 提交于 2019-12-02 08:26:26
I'm trying to learn how to use bitwise operators on a given input but am not having much luck figuring out how to use them. Let's say I have this following octet: 11(01)0000 How would I extract the bits between the braces? You need to: create a suitable mask with ones only where are the bytes you need (you just need to write the number in binary and convert to e.g. hex to put it inside the C program). The parentheses in your 11(01)0000 are your indication to where to put the ones in your mask. Alternatively, create a mask made of as many ones as the chunk of bits you are interested in (in your

Find most significant set bit in a long

放肆的年华 提交于 2019-12-02 08:19:42
I'm in the unique situation where searching for "most significant bit" yields too many results and I can't find an answer that fits my needs! The question itself is pretty simple: "How do I find the most significant set bit in an unsigned long?" When I do my calculations the rightmost bit position is position '0'. I know that it involves masking the lowest bit, checking and then shifting left to once while incrementing my count, and then repeating with the 2nd lowest, etc. I've done this before but for whatever reason I can't do it now. Edit: By "most significant" I mean leftmost set bit,