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
1 answer

Bitshifting and making sure the last two digits are 10 in C

How do I bitshift so I only need to compare the first two digits in a number? Say I want to compare 10101011010 the last two bits and make sure it's 10. How would I do that?
jubibanna
  • 1,095
  • 9
  • 21
0
votes
1 answer

Bitshift operations and bitmask not detecting duplicate characters

I've been trying to write a program that checks for duplicate letters in a lowercase input using a bitmask. However, the program returns true regardless of the presence of a duplicate letter. The code is as follows: #include #include…
0
votes
3 answers

Need to convert bit masking in Python to VB.NET

I have some Python 3 code that pulls info from an array of bytes using masking but I need to do it in a VB.NET program. This is the Python. modulation_indicies = ['Unknown','Normal','High','Low'] phs_noise =…
IanOnHill
  • 1
  • 1
0
votes
2 answers

How are these two methods to find what power of 2 a number is, different?

So, let's say I have a number N that's guaranteed to be a power of 2 and is always greater than 0. Now, I wrote two C methods to find what power of 2 N is, based on bitwise operators - Method A - int whichPowerOf2(long long num) { int ret =…
gravetii
  • 9,273
  • 9
  • 56
  • 75
0
votes
0 answers

What happens when you assign a value of greater than 1 to a bit in a register?

What happens if I set a single bit to a value greater than 1 like below? CANCDMOB = ( 1 << CONMOB1) | ( 1 << IDE ) | ( 8 << DLC0); Will the DLC0 bit just be set to 1 or will it affect the next bits in the register aswell? DLC0 is the LSB of an…
0
votes
1 answer

Bitshifting of a number in C

I have to convert a Number of Type Long in C to a BCD (Binary Coded Decimal) Number with 4 Bits each. I can only use Bitwise shifting and Bitoperations. Can anyone help me achiving this. At the moment i have no idea how to reach my goal. unsigned…
user6652930
0
votes
3 answers

arithmetic right shift shifts in 0s when MSB is 1

As an exercise I have to write the following function: multiply x by 2, saturating to Tmin / Tmax if overflow, using only bit-wise and bit-shift operations. Now this is my code: // xor MSB and 2nd MSB. if diferent, we have an overflow and SHOULD…
Duke
  • 386
  • 2
  • 13
0
votes
2 answers

How to cast 10 bit sign integer to 16 bit integer in C

I am parsing a data which is a 10 bit signed integer. Since the only way to represent this data is to either use int or short ( for sign 2-byte representation), I have to cast 10 bit to 16 bit. I have applied 2 methods already but they are either…
0
votes
0 answers

Getting exponent value using bit shifts (C, C++)

If I have FLAG_VAL = 1 << 6; //(or 64), how can I get back the exponent (6 in this case) later in the code using FLAG_VAL and bit shifts? Thank you.
0
votes
1 answer

Theory behind multiplying two numbers without operands

I have been reading a Elements of Programming Interview and am struggling to understand the passage below: "The algorithm taught in grade-school for decimal multiplication does not use repeated addition- it uses shift and add to achieve a much …
VickTree
  • 889
  • 11
  • 26
0
votes
1 answer

Bit-shift behavior

uint16_t a = 0x00 << 8 + 0xB9; printf("%d",a); I'm expecting 185 as an output but I'm getting 0. What is happening here?
0
votes
2 answers

Bit shifts on buffer of arbitrary bit length

I have been looking for functions doing bit shifts on a buffer of arbitrary bit length in C99. There are probably many libraries doing that but I would like to avoid adding dependencies just for that (but I would be OK to extract functions from a…
acapola
  • 1,078
  • 1
  • 12
  • 23
0
votes
1 answer

Problem with converting signed/unsigned int variables in Arduino

I am making a circuit, that will pull data out of the 4011 shift register. So far it works fine, but as my number rolls over some value, strange things start to happen. This code will cycle all of my 4011's. typedef unsigned long u_long; for…
Kralik_011
  • 393
  • 1
  • 3
  • 14
0
votes
1 answer

Convert 12-bit number into 2 8-bit bytes

For the life of me I cannot figure out how to convert a (maximum) 12-bit number into 2 8-bit bytes (where one of the values will obviously not be able to exceed 4-bits). For example, I can convert 7 (the 4-bit value) and 60 (8 bit value) integers…
user5156141
  • 655
  • 9
  • 29
0
votes
1 answer

not able to shift hex data in a unsigned long

i am trying to convert IEEE 754 Floating Point Representation to its Decimal Equivalent so i have an example data [7E FF 01 46 4B CD CC CC CC CC CC 10 40 1B 7E] which is in hex. char strResponseData[STATUS_BUFFERSIZE]={0}; unsigned long strData =…