Skip to content

Instantly share code, notes, and snippets.

@brandonto
brandonto / keybindings.json
Last active June 10, 2024 23:51
VSCode Key Bindings for Vim navigation between Editor/Terminal/FileExplorer
[
// Vim navigation
//
{
"key": "ctrl+h",
"command": "workbench.action.focusLeftGroup",
"when": "editorTextFocus && vim.active && vim.mode != 'Insert' && !firstEditorInGroup"
},
{
"key": "ctrl+l",
@brandonto
brandonto / Timer.cpp
Created September 16, 2016 13:33
Basic ThreadPool Implementation
//******************************************************************************
//
// Timer.cpp
//
// Copyright (c) 2016 Brandon To
// This code snippet is licensed under the BSD 3-clause license.
//
// Author: Brandon To
// Created: August 24, 2016
//
#include <pthread.h>
#include <sched.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <iostream>
#include <bitset>
@brandonto
brandonto / windows_socket_udp.cpp
Created November 1, 2015 00:56
Windows Socket UDP
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <winsock2.h>
#include <Windows.h>
#include <stdint.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "Ws2_32.lib")
@brandonto
brandonto / windows_socket_tcp.cpp
Last active October 31, 2015 21:07
Windows Socket TCP
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <winsock2.h>
#include <Windows.h>
#include <stdint.h>
#include <Ws2tcpip.h>
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "Ws2_32.lib")
@brandonto
brandonto / SocketTimeout
Created May 12, 2015 19:21
Handling InterruptedIOException for Socket Timeout
// Socket used for receiving packets
DatagramSocket receiveSocket = new DatagramSocket(Constants.SERVER_PORT);
// Socket will time out after 1 second
receiveSocket.setSoTimeout(1000);
try
{
// Creates and start loop to quit on input q
Thread quitThread = new Thread(
@brandonto
brandonto / TestByteIntConversion.java
Last active August 29, 2015 14:20
Encodes an int as 2 bytes
// For Tianming
// Encodes an int as 2 bytes
public class TestByteIntConversion
{
public static void main(String[] args)
{
int blockNumber = 257;
System.out.println("Block number = " + blockNumber);
wget -np -r --reject "index.html*" http://www.webpagetodownload.com/path/to/folder
@brandonto
brandonto / gist:f36df9b948e5e1d3bb73
Created February 24, 2015 00:23
Setting/Getting a specific bit in an unsigned char
/// x: 8-bit value. k: bit position to set, range is 0-7. b: set bit to this, either 1 or 0
unsigned char SetBit(unsigned char x, unsigned char k, unsigned char b) {
return (b ? (x | (0x01 << k)) : (x & ~(0x01 << k)) );
// Set bit to 1 Set bit to 0
}
unsigned char GetBit(unsigned char x, unsigned char k) {
return ((x & (0x01 << k)) != 0);
}
@brandonto
brandonto / gist:0c72c73e39dcc4df4d2a
Last active August 29, 2015 14:15
An array is cast to a pointer when passed as a parameter to a function
#include <stdio.h>
void initialize_array(int arr[], int val);
int main()
{
int arr[30];
initialize_array(arr, 5);