1

I have binary numbers (32 / 64 bit length) which I want to shift to the left so that the leading significant bit disappears. All the binary numbers are of different length. So I did not find an easy way to do it.

Here are some examples. On the left side are the binary numbers before and on the right side the binary numbers after the left shift:

00000000000000000000000000101011011 -> 01011011000000000000000000000000000
00000000000000000000100010110111011 -> 00010110111011000000000000000000000
00000000000000000000000000000100110 -> 00110000000000000000000000000000000
00000000000000111000101010101100010 -> 11000101010101100010000000000000000

How can one do that in C?

Gilfoyle
  • 3,282
  • 3
  • 47
  • 83
  • @Downvoter I am a beginner in C. So I do not understand most of what I see in this other post. Is there no simple way (example which is easy to understand) so that a beginner can work with it? – Gilfoyle Dec 03 '16 at 18:22
  • The other post gives you a set of functions that tell you how many leading zeros a value has. You can then shift left by that value plus 1. – dbush Dec 03 '16 at 19:13
  • @dbush I tried to solve my problem with three different solutions of that post. Unfortunately it does not work for me. The output is not of the form what I expected. One solution returns `11111111111111111010100110111001` and another solution returns `00000000000000000000000000000110` when I pass `0000000000000000010100110111001`. Can someone give me a hint what I am doing probably wrong? – Gilfoyle Dec 03 '16 at 22:06
  • You'll need to show you code for anyone to know what that might be. Post it in a new question. – dbush Dec 03 '16 at 22:09

1 Answers1

-3
uint64_t number;

for(int i = 0; (i < 64) && (number & 0x8000000000000000) == 0); i++)
{
    number <<= 1;
}

number <<= 1;

This is one way of doing it for 64 bit number value;

Fixed.

koper89
  • 645
  • 3
  • 9