Questions tagged [bit-shift]

A bit shift operation moves the bits contained in a binary numeral or bit pattern to the left or to the right.

A bit shift operation moves the bits contained in a binary numeral or bit pattern to the left or to the right, often written as << and >> respectively.

For example 4<<3 = 32 and 5>>1 = 2.

A zero shift returns the integer part, e.g. 3.14<<0 = 3.

2051 questions
0
votes
2 answers

Is there a more simple, less machine-level way of expressing or doing the same as (i & (1 << j))?

I found this bit of code (no pun intended) that solves a problem I needed to solve but I don't really understand what the (i & (1 << j)) part does. Is there a more "high level" way of writing it so I can understand it? Or a different way of doing…
Martin
  • 47
  • 1
  • 6
0
votes
2 answers

How to change uint16_t to two uint8_t?

How to convert one uint16_t to two parts? uint16_t value = 0x7133; uint8_t partA = (uint8_t)((value & 0xFF00) >> 8); uint8_t partB = (uint8_t)(value & 0x00FF); std::cout << std::hex << partA << std::endl; std::cout << std::hex << partB <<…
Skanda
  • 145
  • 4
  • 14
0
votes
4 answers

Kotlin decomposing numbers into powers of 2

Hi I am writing an app in kotlin and need to decompose a number into powers of 2. I have already done this in c#, PHP and swift but kotlin works differently somehow. having researched this I believe it is something to do with the numbers in my code…
Drewby80
  • 31
  • 6
0
votes
2 answers

Chain value of string variable with some text and print it to standard output in C++

I want to do something really simple: I have function that has string parameter and I want to chain it to some constant string, then output result to console like this: void test(string s){ cout << "Parameter of this function was: " << s; } In…
Rasto
  • 17,204
  • 47
  • 154
  • 245
0
votes
1 answer

An efficient way to calculate extremely large powers of 2

I am solving a problem which requires me to calculate the sum of squares of all possible subsets of a set. I am required to return this sum, modulo 10^9+7 I have understood the logic. I just need to sum the squares and multiply the result by 2^N-1,…
Karan Singh
  • 1,114
  • 1
  • 13
  • 30
0
votes
1 answer

How to properly make a bitwise shift in this 3 cases?

I am revising some of my learning resources and I have came across 2 examples that are somehow tricky for me. 1) The result of bitwise shift for an int value 0x80000000>>8 is 0x800000. How to make 8 shifts right when number of digits after 0x is…
S.Rucinski
  • 109
  • 1
  • 8
0
votes
3 answers

Unclear precedence of bitshift operation

I've been struggling with the shift operator in PHP, assuming that it precedes arithmetic operations like +, 1 etc. I've been unable to find any definition of this in the php manual. Let's say that I have the value 1, which I want to 3, then to 7…
0
votes
4 answers

Re-Indexing Bits Within a Char

I have an exercise where I have to encode and decode strings at the bit level that are given in by the command line. The caveat for this is that I have to use a permutation mapping to re-order the bits. Here's an Example: The User Inputs The…
user11402812
0
votes
1 answer

C++ bit manipulation compiles with no errors on my system but godbolt compilers give warning/errors

Edit: Godbolt link for example here! So I've got this example to show the macros I've made and their use case: #include #include #define bit_mask(size, offset) (~(~0 << size) << offset) #define bit_masked_set(dst, src, mask,…
Hex Crown
  • 753
  • 9
  • 22
0
votes
1 answer

Don't understand how these bitwise operators operate on bytes and integers

I am working with some code that takes in a binary file as input. However, I am having trouble understanding the for loop in the code, as I don't understand what the bitwise operators do to IFD_Address, such as the |=, <<, and & 0xff. I think…
dcs
  • 31
  • 4
0
votes
1 answer

Why does a right arithmetic shift of 3 to the binary sequence "0110 0100" lead to "1110 1100" instead of "0000 1100"?

In a textbook I'm looking at, it asks me to do an arithmetic right shift to the binary sequence 0110 0100 by 3 (a >> 3). I thought it was 0000 1100 as I would add 3 zeros at the right considering the most significant bit is 0 (or is it 01?), but the…
0
votes
3 answers

How do I use bitwise shifts with cout?

I'm trying to do something similar to this: #include int main() { std::cout << 1 << 5 << std::endl; } I expect 32 (1 shifted left by 5), but I get 15. I am trying to use a macro like this: #define BIT_SHIFT(x,y) x << y ... cout <<…
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
0
votes
1 answer

bitshifting does not propertly work for short values

I've a little problem with bitshifting short values: System.out.println(Integer.toBinaryString(0b1100010001000100 >>> 12); // works correctly, output: 1100 System.out.println(Integer.toBinaryString( 0xFFFF & (short)(0b1100010001000100) >>> 12)) //…
Metalhead
  • 81
  • 2
  • 7
0
votes
1 answer

shift right and shift left assembly language

If I want to say for example bx is a number: shl bx,1 shr bx,1 What will be the new bx value? Does it stay the same?
0
votes
2 answers

What is the time complexity of this division function (no divide or multiply operators used)?

I solved this leetcode question https://leetcode.com/problems/divide-two-integers/ . The goal is to get the quotient of the division of dividend by divisor without using a multiplication or division operator. Here is my solution: def…