Flipping bits
Flipping bits
bcrawford + 3 comments Forgive me if I am mistaken, but the discussion board should be for discussion of the problems not for users to randomly post their solutions...
mjk + 3 comments You'd be surprised to learn that there can be different solutions to the same problem.
For example, in 'Lonely Integers', I had a solution that worked. But then I saw there was a much more simpler answer that would run faster and I learned something new. :)
niuworld + 0 comments Exactly
AllisonP + 1 comment Totally agree, and I'd like to add that links to full submissions will not display for users who have not yet submitted a solution to the problem.
wittrup + 1 comment How about a page that automatically picks a selection of the successful submissions? For each language, with one criteria as the shortest amount of code, another criteria being the fastest code, another one-liners etc. So that when you have submitted solutions to the problem, and are really stuck and just want to look at answers you can buy you into a page showing all that.
7andrexu7 + 0 comments codeforces shows the size utilized by your algorithm and time elapsed.....You can filter the languages too
dyesdyes + 2 comments That is why there is an editorial (now, not sure about 2 years ago though)
AnonymousNovice + 0 comments sometimes other users provide good and much smarter solution then editorial.
Kanudo_10 + 0 comments [deleted]
JKDos + 0 comments Sharing source code is great. Not only will you learn new ways, or even teach new ways. You also will get the lightbulb turned on.
Sometimes, the chanllenges are worded in a way that you can't understand what's being ask, or maybe you just have no clue. Coming to to discussion board, if you happen to find a solution, you can read it through and understand what's being done. AND BOOM the lightbulb comes on. Then rewrite it to your own language.
Doing so, you've just learned something new. Either way.AnonymousNovice + 0 comments Solution is posted for review. Somebody can suggest some improvement also.
sussmansa + 2 comments Ohh wow, just figured it out. The first number is the number of inputs. So the example is 3 values, value1, value2, value3. The description is just bad.
gracelovesrmit + 0 comments wow.....that makes sense....
XeroOl + 4 comments java doesn't do unsigned integers, this was a pain
anaspk + 6 comments Exactly!! After wasting some 15-20 minutes in Java, I switched to C++ and it was done in 1 minute :(
ak278anand + 8 comments Simple solution in java
private static long complement(long num){ return maxInt - num; }
Where long maxInt=(long)Math.pow(2, 32)-1;
guilhebl + 1 comment or long maxInt = Integer.MAX_VALUE;
ststhh + 1 comment Integer.MAX_VALUE == Math.pow(2, 31)-1 and we need Math.pow(2, 32)-1
csmattjohnston + 1 comment Why is it Math.pow(2, 32)-1 instead of Math.pow(2, 31)-1?
bcwfs + 0 comments It's the difference between signed and unsigned numbers. Signed numbers take one bit to save the -/+ state and thus the max number would be something like 0111...1 (since the first bit is usually the sign bit) not 1111...1
giyer + 1 comment Why does that work? I'm not sure I understand.
AllisonP + 1 comment If maxInt is the maximum value an integer can have, subtracting the original number will give you the complement. For example: If the input number is 6, that's 110 (00000110) in binary. 11111111 - 00000110 = 11111001, which, as you can see, is the input value's flipped bits.
csmattjohnston + 0 comments Thank you for explaining!
pankaj113 + 2 comments Solution passes two of the nine test cases
osoriomar + 0 comments Are you using a bit mask to solve the problem?
ishtiaque_h + 1 comment Happned to me, too. Later had to buy input-output to realize that the problem statement says input could be 2^32. That you cannot fit into an integer. Try reading the input as long. That worked for me. Thanks.
saif01md + 0 comments but long is also 4 bits...we have to use unsigned long same as unsignned int ? pleasr help
bnegrao + 1 comment Why does the integers must be declared as "long"? why can't them just be 'int'? isn't int supposed to represent 32 bits number? ...
ajboss + 0 comments int ranges from −2,147,483,648 to 2,147,483,647
unsigned int ranges from 0 to 4,294,967,295
the size is same but its the values that change!
simply read it as signed int and unsigned int. you'll get an idea.
Leonina + 4 comments Or if you want to do it with bitwise operators in java:
long num = in.nextLong();
long maxValue = Math.pow(2,32)-1;
System.out.println(num ^ maxValue);
abhi_net5202088 + 0 comments cool. just few lines solution.
vincent_g + 0 comments Great idea, that num ^ maxValue.
Aktan + 0 comments Or, you could do num ^ 0xFFFFFFFFL instead!
Sarthak10 + 0 comments Math.pow() returns a 'double' value,so you need to typecast it to 'long' before putting it into maxValue.
kevin_lin790 + 0 comments So clever. Would +20 it if I could.
(I made a solution in Python using bit operations, come to find out I never needed it.)
lonewolff + 0 comments i did the same thing but in a twisted way. Yours was much simpler
return ~n<0?(long)Math.pow(2,32)+~n:~n;
sushmabiswas7 + 0 comments Thanks very much for this!
My initial implementation was timing out - too crude :/
This is MUCH better:
def flippingBits(n): flipped_n=(pow(2,32)-1)-n return flipped_n
A_N_P + 0 comments thats true
javierchacana + 0 comments Same here :(
emaskovsky + 5 comments Yes indeed, the solutions in other languages seem pretty crazy.
In C++, I just do
unsigned x; cin >> x; cout << ~x << endl;
Done :D
MistaTwist + 1 comment I tried this initially in JavaScript, and instantly failed. Good place for me to start, though, I guess!
Kevinagin + 1 comment To do this in JavaScript, you need to end your operation with a Zero-fill right shift (>>>) so the result will be treated as an unsigned integer.
Not quite as straight-foward, but it should still be only be one line of code: ~n >>> 0.
darkjedi + 0 comments I didn't understand >>> until now. You are a star :+1:
sahildhawan22 + 2 comments That's what I thought but I used int x, instead of unsigned. Can you explain why it didn't worked?
emaskovsky + 1 comment Depends how you print it. The bits are likely correct, buut the interpretation of int in cout is indeed different than interpretation of unsigned.
I.e. with int, you'd need to cast it to unsigned just for the printing, for example:
int x; ... cout << static_cast<unsigned>(~x) << endl;
(and indeed if there is some number in the input which is greater than the max signed int, i.e. 2147483647, it will not work as well because of that - and such numbers are a valid input according to the description)
sahildhawan22 + 0 comments But doing other bit manipulation questions, I have seen it correctly resembles the decimal value. Here for 5 (101), it should print 2 (010), but output I get is -1.
maximshen + 0 comments This at least shows how C++ is more handy than Java
osoriomar + 2 comments That looks pretty straightforward!
What is the name of the "~" expression? Reverse? Inverse?
osoriomar + 0 comments I just added a bitmask for the largest 32 bits possible number and apply XOR to get the new number. It's not very elegant but it worked.
string current = Console.ReadLine(); Console.WriteLine(4294967295^long.Parse(current));
crystalgong123 + 0 comments "~" (tilde) is the bitwise NOT operator.
harshitagarwal37 + 1 comment does
~
also changes the sign of the number ??mgperson + 1 comment It does if you're using a normal (signed) int. One bit is reserved for the sign and since you're flipping each bit you by definition are flipping the sign bit.
harshitagarwal37 + 1 comment which bit stands for sign change ?
mgperson + 0 comments The sign bit is always the most significant bit in the bit representation of the number. Generally the convention is to write the MSB as the leftmost, but this isn't always the case: https://en.wikipedia.org/wiki/Most_significant_bit For example, the 8 bit number 10001001 = -9. The leftmost 1 indicates a negative number.
abhilash_challa + 0 comments why doesnt the same work in C ?
AllisonP + 1 comment I completed it in Java by using longs and complement addition.
~n + 0x0000000100000000L
heerokenshin + 0 comments that was very slick of your part! cheers!
mitrandir + 0 comments After reading this i switched to go and was ready under 1 minutes as well :) Thank you anaspk !
return int64(uint32(n)^(^uint32(0)))
tainingw + 0 comments [deleted]xiaohanyu + 0 comments Java 8 has a new
Integer/parseUnsignedInt
function, check http://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseUnsignedInt-java.lang.String-rajmalhotra91195 + 0 comments You can use long,no problem I had done it using long.
Soumyadeep_B + 3 comments 1 XOR anything gives its inverse. Hence, for 32 bit unsigned numbers , (2^32 -1) XOR 'the given number' flips each bit. I guess its possibly the fastest way to get this done.
subho_mukherjee + 0 comments 100 %
scarpenter + 0 comments Faster then simply using the ~ (one's complement) operator? I guess I thought maybe the compiler optimized a action so it wouldn't matter.
charvakcpatel007 + 1 comment return ~x;
shrikanthmethre1 + 1 comment kindly give the description properly
s1w2 + 1 comment first you can take value from user and there is 3 point which you have to take care.... 1-input should convert into binary 2-binary value have to flipp means where 1 put 0 and where 0 put 1 3-You got flipped number now again you have to convrt it into decimal ,the ligic are... for (int i = arr.Length - 1; i >= 0; i--) {
uint x = arr[i]; uint r = x; if (x == 1) { value = Convert.ToUInt32(Math.Pow(2, i)); val += value; } else { if (r == 0) { } else { value = Convert.ToUInt32(Math.Pow(0, i)); val += value; } } }
this is in c# program ....you can do it in c and c++ Thank You Swatantra Mishra
ektajaiswal3mar + 1 comment Can be done using NOT gate
int T; cin >> T; unsigned int *n = new unsigned int[T]; for(int i = 0; i<T; i++) { cin >> n[i]; } for(int i = 0; i<T; i++) { cout << ~n[i] << "\n"; }
keyurhariya + 0 comments This solutions works fine but using for loops adds a lot of processor cycles. Also, no need to store all the numbers from input, you can save memory by processing one test case at a time. Alternative to same solution but speed and memory efficient:
int T; unsigned int n; cin >> T; while(T--){ cin >> n; cout << ~n << endl; }
arun_morampudi + 0 comments just noticed.,its a single line code as the output we seek is the difference b/w (2^32)-input_n. This aint worth 40 points :P
akeim + 0 comments Simple solution in javascript after shifting the first item off the array
return(~integer >>> 0)
Basically it shifts the inverted array to the right, and inputs zero as the sign bit, making the result the equivalent of an unsigned int in other languages.
abhishek106 + 4 comments n = int(input()) for _ in range(n): val = int(input()) print(val^4294967295)
kalez + 0 comments nice,
KL30246 + 0 comments Can u explain me how this works bro? i mean... how did u get that idea!!?
the_dhrubo + 0 comments just do it :
return n ^ 4294967295
dm_dubovitsky + 0 comments Easy to replace last line for:
print(val^0xffffffff)
mayank7577 + 0 comments here is my code of in O(1)-
long flippingBits(long n) { long res = n^(0xffffffff); return res ; }
jairamg + 2 comments Hi all, I am getting a 'runtime error' after code submission. I am pretty sure it is not a timing issue. Any idea why this is the case?
[deleted] + 1 comment Same, my solution is correct too, as I've tested it. Submited using java
rkbansal83 + 0 comments I am also getting runtime error after code submission. It works fine on my local. Submitted using java
Sort 316 Discussions, By:
Please Login in order to post a comment