Open the settings (Host Key (usually right ctrl) + s) > Advanced > Shared Clipboard > Enable
Fire the below command and restart the VM
sudo apt-get install virtualbox-guest-dkms
Hope this helps!
/* * HC-SR04 <> RPi * To calculate the distance * Using the libbcm2835 library * Author : Ahamed EN (ahamed.en@gmail.com) * 20-March-2016 */ #include <bcm2835.h> #include <stdio.h> #include <sys/time.h> /* We will be using * Pin#16 (GPIO23) as Trigger * Pin#18 (GPIO24) for Echo */ #define TRIGGER RPI_GPIO_P1_16 #define ECHO RPI_GPIO_P1_18 int main(int argc, char **argv) { if (!bcm2835_init()) return 1; struct timeval start, end; float duration; bcm2835_gpio_fsel(TRIGGER, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_fsel(ECHO, BCM2835_GPIO_FSEL_INPT); bcm2835_gpio_write(TRIGGER, LOW); bcm2835_gpio_write(ECHO, LOW); bcm2835_delay(2); bcm2835_gpio_write(TRIGGER, HIGH); usleep(10); /* As per the HC-SR04 manual */ bcm2835_gpio_write(TRIGGER, LOW); /* Wait until we get a HIGH */ while(!bcm2835_gpio_lev(ECHO)); gettimeofday(&start, NULL); /* Wait until we get a LOW */ while(bcm2835_gpio_lev(ECHO)); gettimeofday(&end, NULL); /* * speed = distance/time * speed = 340m/s = 34300cm/s (speed of sound) * * 34300 = distance/time * * distance = time/2 * 34300 (time/2 because what we got is the time * for to and fro) */ duration = (end.tv_sec - start.tv_sec) * 1000.0; duration += (end.tv_usec - start.tv_usec) / 1000.0; duration = duration/1000; /* to seconds */ float distance = duration/2 * 34300; printf("Disatnce : %f cm\n", distance); bcm2835_delay(500); bcm2835_close(); return 0; }
root@raspberrypi:~/sim# gcc send.c -o send_sms root@raspberrypi:~/sim# ./send_sms Writing to Port : AT Read from port : ATOK Writing to Port : AT+CMGF=1 Read from port : AT+CMGF=1OK Writing to Port : AT+CNMI=2,1,0,0,0 Read from port : AT+CNMI=2,1,0,0,0OK Writing to Port : AT+CMGS="9535213788" Read from port : AT+CMGS="9535213788"> Writing to Port : PI_SIM800_TESTING
/* * SIM800 <> Raspberry Pi - Serial Interface * To send a SMS using SIM800 module via RPi * Author : Ahamed EN (ahamed.en@gmail.com) * Date : 20 March 2016 */ #include <stdio.h> #include <string.h> #include <fcntl.h> #include <termios.h> /* This is R-Pi default serial port */ #define DEFAULT_PI_SERIAL_PORT "/dev/ttyAMA0" typedef enum _mode_t{ RW, WO, RO }sim_mode_t; static inline void error(char *err) { printf("Error : %s\n", err); } #define ERROR(err) { printf("[%s %d] ", __func__, __LINE__); error(err); } int open_serial_dev(char *serial_dev) { if(!serial_dev){ ERROR("serial_dev in NULL"); return -1; } int fd; fd = open(serial_dev, O_RDWR | O_NOCTTY | O_NDELAY); if(fd < 0){ ERROR("Could not open the serial device"); return -1; } return fd; } /* To initialize the serial port via termios */ int serial_init(int fd) { struct termios termios; int ret = 0; ret = tcgetattr (fd, &termios); if(ret < 0){ ERROR("tcgetattr failed"); return -1; } cfsetispeed(&termios, B9600); cfsetospeed(&termios, B9600); termios.c_iflag &= ~(IXANY | IXON | IXOFF); termios.c_lflag &= ~(ICANON | ECHO); termios.c_cflag |= (CLOCAL | CREAD); termios.c_cc[VMIN] = 1; termios.c_cc[VTIME] = 10; ret = tcsetattr (fd, TCSANOW, &termios); if(ret < 0){ ERROR("tcsetattr failed"); return -1; } return 0; } void set_cmd(char *cmd, char *cmd_str) { memset(cmd, 0, sizeof(cmd)); strcpy(cmd, cmd_str); } int read_port(int fd, int ret) { int rv = 0; int i = 0; char buf[64]; if(ret){ rv = read(fd, &buf, sizeof(buf)); printf("Read from port\t: "); for(i=0; i<rv; i++){ if(buf[i] == '\n' || buf[i] == '\r') continue; printf("%c", buf[i]); } printf("\n"); } return rv; } int write_port(int fd, char *cmd, int len) { if(fd < 0){ ERROR("Invalid fd"); return -1; } printf("Writing to Port\t: %s", cmd); int ret = 0; ret = write(fd, cmd, len); if(ret <= 0){ ERROR("write failed"); } return ret; } /* Function to write and read from the port */ void set_get_cmd(int fd, sim_mode_t mode, char *cmd_str) { int ret; char cmd[64]; set_cmd(cmd, cmd_str); ret = write_port(fd, cmd, strlen(cmd)); usleep (100000); if(mode == WO){ return; } read_port(fd, ret); return; } int main(int argc, char **argv) { char *serial_dev = DEFAULT_PI_SERIAL_PORT; int ret = 0; int fd; if(argv[1]){ serial_dev = argv[1]; } fd = open_serial_dev(serial_dev); if(fd < 0){ ERROR("Exiting"); return -1; } ret = serial_init(fd); if(ret < 0){ ERROR("Exiting"); return -1; } /* Check the SIM800 Data sheet for the commands */ set_get_cmd(fd, RW, "AT\r\n"); set_get_cmd(fd, RW, "AT+CMGF=1\r\n"); set_get_cmd(fd, RW, "AT+CNMI=2,1,0,0,0\r\n"); /* Specify the 10 digit mobile # here */ set_get_cmd(fd, RW, "AT+CMGS=\"9535213788\"\r\n"); /* Following is your text message */ set_get_cmd(fd, WO, "PI_SIM800_TESTING\r\n"); set_get_cmd(fd, WO, "\x1A"); printf("\n"); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
#include "stdio.h" #include "stdlib.h" #include "string.h" #include "time.h" typedef enum _return_type { ERROR, SUCCESS }return_type_t; #define NULL_CHECK(x) { if(!x) return ERROR; } static int merge(int *a1, int n1, int *a2, int n2, int *a) { int i=0, j=0, k=0; while(i<n1 && j<n2){ if(a1[i] <= a2[j]){ a[k] = a1[i]; i++; } else{ a[k] = a2[j]; j++; } k++; } while(i<n1){ a[k] = a1[i]; i++; k++; } while(j<n2){ a[k] = a2[j]; j++; k++; } return SUCCESS; } static int merge_sort(int *a, int m) { int *a1, *a2; int n1, n2; if(m<2) return SUCCESS; n1 = m >> 1; n2 = m - n1; a1 = malloc(sizeof(int) * n1); NULL_CHECK(a1); a2 = malloc(sizeof(int) * n2); NULL_CHECK(a2); int i=0; for(i=0; i<n1; i++) a1[i] = a[i]; for(i=0; i<n2; i++) a2[i] = a[i+n1]; merge_sort(a1, n1); merge_sort(a2, n2); merge(a1, n1, a2, n2, a); free(a1); free(a2); return 0; } static void display(int *a, int n) { int i; for(i=0; i<n; i++){ printf("%d ", a[i]); } printf("\n"); return; } int main(int argc, char **argv) { int *a; int i,n,num; scanf("%d", &n); a = malloc(sizeof(int) * n); NULL_CHECK(a); srand ( time(NULL) ); for(i=0; i<n; i++){ num = rand() %1000 + 1; a[i] = num; } //display(a, n); merge_sort(a, n); //display(a, n); free(a); return 0; } |
root@maximus:~# uname -a
Linux maximus 3.12-kali1-686-pae #1 SMP Debian 3.12.6-2kali1 (2014-01-06) i686 GNU/Linux
--enable-ssl --enable-ssl-crtd --enable-icap-client --with-default-user=squid
wget http://www.squid-cache.org/Versions/v3/3.4/squid-3.4.4.tar.gz
tar -xzvf squid-3.4.4.tar.gz
cd squid-3.4.4
./configure --prefix=/ --enable-icap-client --enable-ssl --enable-ssl-crtd --with-default-user=squid
make
make install
root@maximus-neptune-21:~# squid -v Squid Cache: Version 3.4.4 configure options: '--enable-ssl' '--enable-icap-client' '--enable-ssl-crtd' '--with-default-user=squid' '--prefix=/' --enable-ltdl-convenience
root@maximus-neptune-21:~# squid -h
Usage: squid [-cdhvzCFNRVYX] [-s | -l facility] [-f config-file] [-[au] port] [-k signal]
-a port Specify HTTP port number (default: 3128).
-d level Write debugging to stderr also.
-f file Use given config-file instead of
/etc/squid.conf
-h Print help message.
visible_hostname maximus-neptune-21 # This acl will allow anyone. Add the required acl's you want to restrict the access. acl EVERYONE src all # This is to tell the Proxy that do not cache the pages. This is not generally used. cache deny all # This is where the acl is getting into action http_access allow EVERYONE http_access deny all # 3128 is the https port squid proxy will listen to. You need to generate the key and certificate, these are used to create a secure connection with the client. I am using SSLv3 and hence version=3 https_port 3128 intercept ssl-bump cert=/etc/squid/ssl/public.pem key=/etc/squid/ssl/private.pem version=3 # This is where we specify the server list. I am using SSLv3 and hence sslversion=3 cache_peer 100.1.1.11 parent 443 0 no-query originserver ssl sslversion=3 ssloptions=NO_SSLv2,NO_TLSv1_1,NO_TLSv1_2 # I had some issues with the CA, hence disabled the verification. I don't recommend you do this if it a live environment. sslproxy_cert_error allow all sslproxy_flags DONT_VERIFY_PEER,NO_DEFAULT_CA # This will ensure that the connection is send/created to the actual server. never_direct allow all
squid -NCd1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #!/bin/bash # utc_to_sec v1.0 # Script to convert time to seconds # TZ should be UTC # # ./utc_to_sec 2014-02-20 01:42:02 # 1392860522 EXE=${0#*/} usage() { echo -e "\nConvert date to seconds since epoch" echo " Usage : $EXE yyyy-mm-dd hh:mm:ss" echo -e " ** TZ default UTC **\n" exit } do_everything() { echo $1 $2 | awk ' BEGIN{ year=hh=1; mon=mm=2; day=ss=3 m_lo[4]=m_lo[6]=m_lo[9]=m_lo[11]=1 m_hi[1]=m_hi[3]=m_hi[5]=m_hi[7]=m_hi[8]=m_hi[10]=m_hi[12]=1 } function no_of_days_in_year(input) { leap=0 if ( input%4 == 0 ){ if ( input%100 == 0 ){ if ( input%400 == 0 ){ leap=1 } } else{ leap=1 } } return (365+leap) } function no_of_days_in_month(in_mon, in_year) { if(m_hi[in_mon]) val=31 else if(m_lo[in_mon]) val=30 else { val= no_of_days_in_year(in_year)>365?29:28 } return val } function validate(a_date, a_time) { if( a_date[year]<1970 || a_date[mon]<=0 || a_date[mon]>12 || a_date[day]<=0 || a_date[day]>31 || ( m_lo[a_date[mon]] && (a_date[day]>30) ) || ( a_date[mon]==2 && a_date[day]>29 ) || a_time[hh]<0 || a_time[hh]>23 || a_time[mm]<0 || a_time[mm]>=60 || a_time[ss]<0 || a_time[ss]>=60 ){ print -1 exit } if(no_of_days_in_year(a_date[year])<=365 && a_date[mon]==2 && a_date[day]>28){ print -2 exit } } END{ split($1, a_date, /-/) split($2, a_time, /:/) validate(a_date, a_time) #no of days in a year for(i=1970; i<a_date[year]; i++) sum_days+=no_of_days_in_year(i) for(i=1; i<a_date[mon]; i++) sum_days+=no_of_days_in_month(i, a_date[year]) sum_days=sum_days+a_date[day]-1 seconds = (sum_days*24*60*60) + (a_time[hh] * 3600) + (a_time[mm] * 60) + a_time[ss] print seconds } ' } ( [[ $# -ne 2 ]] ) && usage seconds=$( do_everything $* ) [[ ${seconds:-0} -lt 0 ]] && echo "Invalid data..." && usage echo $seconds |
root@maximus:~/scripts# ./utc_to_sec 2037-12-31 23:59:59 2145916799 root@maximus:~/scripts# date +%s -d "2037-12-31 23:59:59 UTC" 2145916799 root@maximus:~/scripts# ./utc_to_sec 2014-02-19 12:13:56 1392812036 root@maximus:~/scripts# date +%s -d " 2014-02-19 12:13:56 UTC" 1392812036 root@maximus:~/scripts# ./utc_to_sec 1970-01-01 00:00:00 0 root@maximus:~/scripts# date +%s -d "1970-01-01 00:00:00 UTC" 0 root@maximus:~/scripts# date +%s -d "1990-01-01 23:40:44 UTC" 631237244 root@maximus:~/scripts# ./utc_to_sec 1990-01-01 23:40:44 631237244
root@maximus:~/scripts# time date +%s -d "2037-12-31 23:59:59 UTC" 2145916799 real 0m0.010s user 0m0.004s sys 0m0.008s root@maximus:~/scripts# time ./utc_to_sec 2037-12-31 23:59:59 2145916799 real 0m0.023s user 0m0.012s sys 0m0.004s
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #!/bin/bash # http://linuxcalling.blogspot.in/ # Script to find the IP address range # Script : ip_range # Usage : ip_range <IP> <Subnet> [0|1 print range] # Ahamed (ahamed.en@gmail.com) usage() { EXE=${0#*/} echo "Usage: $EXE <IP> <Subnet> [0|1]<print range>" echo " eg: To display the IP address range" echo " $EXE 192.168.1.1 255.255.140.0" echo " eg: To display the IP address range with entire range" echo " $EXE 192.168.1.1 255.255.140.0 1" exit } [ $# -lt 2 ] && usage echo $1 $2 $3 | awk ' BEGIN{ BITS=8 mask_add[0]=valid_subnet[0]=0 for(i=BITS-1;i>=0;i--){ k=or(k,lshift(1,i)) mask_add[k]=BITS-i valid_subnet[BITS-i]=k } } function validate_subnet(input) { split(input, oc, ".") if(oc[4] > oc[3] || oc[3] > oc[2] || oc[2] > oc[1]){ print "Invalid Subnet: ",input exit } for(j=1; j<=BITS/2; j++){ for(i=0;i<=BITS;i++){ if(!xor(oc[j],valid_subnet[i])){ break; } err++ } if(err == (BITS+1)){ print "Invalid Subnet: ",input exit } err=0 } } function to_cidr(input) { split(input, oc, ".") ret=mask_add[oc[1]]+mask_add[oc[2]]+mask_add[oc[3]]+mask_add[oc[4]] return ret } function ip_to_int(input) { split(input, oc, ".") ip_int=(oc[1]*(256^3))+(oc[2]*(256^2))+(oc[3]*(256))+(oc[4]) return ip_int } function int_to_ip(input) { str="" num=input for(i=3;i>=0;i--){ octet = int (num / (256 ^ i)) str= i==0?str octet:str octet "." num -= (octet * 256 ^ i) } return str } { ip=$1 mask=$2 range=$3 validate_subnet(mask) cidr=to_cidr(mask) diff=32-cidr no_of_ip=2^diff ip_int=ip_to_int(ip) ip_mask=and(ip_int, (2^32 - 2^diff)) ip_start=ip_mask ip_end=ip_mask+no_of_ip-1 print "No of IPs\t"(no_of_ip-2) print "First IP\t" int_to_ip(ip_start+1) print "Last IP\t\t" int_to_ip(ip_end-1) print "Subnet\t\t" int_to_ip(ip_mask) print "Broadcast\t" int_to_ip(ip_end) if(range){ print "\nIP address range -- START --\n" curr=ip_start i=0 while(curr<=ip_end){ printf int_to_ip(curr)"\t" if(j++%4 == 0) printf "\n" curr+=1 } print "\nIP address range -- END --" } } ' |
[root@maximus] → ./ip_range 192.168.1.1 255.255.255.224 No of IPs 30 First IP 192.168.1.1 Last IP 192.168.1.30 Subnet 192.168.1.0 Broadcast 192.168.1.31 [root@maximus] → ./ip_range 192.168.1.1 255.255.255.224 1 No of IPs 30 First IP 192.168.1.1 Last IP 192.168.1.30 Subnet 192.168.1.0 Broadcast 192.168.1.31 IP address range -- START -- 192.168.1.0 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 192.168.1.5 192.168.1.6 192.168.1.7 192.168.1.8 192.168.1.9 192.168.1.10 192.168.1.11 192.168.1.12 192.168.1.13 192.168.1.14 192.168.1.15 192.168.1.16 192.168.1.17 192.168.1.18 192.168.1.19 192.168.1.20 192.168.1.21 192.168.1.22 192.168.1.23 192.168.1.24 192.168.1.25 192.168.1.26 192.168.1.27 192.168.1.28 192.168.1.29 192.168.1.30 192.168.1.31 IP address range -- END -- [root@maximus] → ./ip_range 192.168.1.1 255.255.0.0 No of IPs 65534 First IP 192.168.0.1 Last IP 192.168.255.254 Subnet 192.168.0.0 Broadcast 192.168.255.255
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | /* * Ahamed * Using cURL libcurl * * For sending pipelined http requests * Usage : ./send_pipelined_http <no of pipelined requests> <http://x.x.x.x> * Some basic validations added. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> static void usage(); #define ERR_LEN 512 #define MIN_URL_LEN 14 #define MAX_URL_LEN 32 #define MIN_PIPELINE_REQ 1 #define MAX_PIPELINE_REQ 32 #define err(x, y) {printf("\nError: %s...\nExiting...\n", x); if(y)exit(0); usage();} static unsigned int pipeline_req; static char url[MAX_URL_LEN]; static char err_str[ERR_LEN]; static void handle_curl_error(CURLcode ret, char *func) { if(ret != CURLE_OK){ fprintf(stderr, "%s() failed: %s\n", func, curl_multi_strerror(ret)); } return; } static void usage() { printf("\nDeveloped by Ahamed using cURL API via libcurl\n"); printf("Usage : ./send_pipelined_http <no of req> <http://x.x.x.x>\n"); printf(" Pipeline Request - Min %d : Max : %d\n", MIN_PIPELINE_REQ, MAX_PIPELINE_REQ); printf(" Hostname not supported yet. Please provide IP address.\n"); printf(" URL - Min Length %d : Max Length : %d\n\n", MIN_URL_LEN, MAX_URL_LEN); exit(0); } static void parse_args(int argc, char **argv) { if(argc <= 2) err("Insufficient arguments", 0); if(!argv) err("Something unexpected happened", 1); pipeline_req = atol(argv[1]); if(pipeline_req > MAX_PIPELINE_REQ || pipeline_req < MIN_PIPELINE_REQ) err("Invalid Pipeline Request", 0); if(strlen(argv[2]) > MAX_URL_LEN || strlen(argv[2]) < MIN_URL_LEN) err("Invalid URL length", 0); strncpy(url, argv[2], MAX_URL_LEN); return; } int main(int argc, char **argv) { int i=0; parse_args(argc, argv); CURLM *m_curl; CURLMcode res; m_curl = curl_multi_init(); curl_multi_setopt(m_curl, CURLMOPT_PIPELINING, 1L); CURL *curl[MAX_PIPELINE_REQ]; for(i=0; i<pipeline_req; i++){ curl[i] = curl_easy_init(); if(!curl[i]) err("Something went wrong", 0); res = curl_easy_setopt(curl[i], CURLOPT_URL, url); handle_curl_error(res, "curl_easy_setopt"); res = curl_easy_setopt(curl[i], CURLOPT_FOLLOWLOCATION, 1L); handle_curl_error(res, "curl_easy_setopt"); res = curl_multi_add_handle(m_curl, curl[i]); handle_curl_error(res, "curl_multi_add_handle"); } int ret = 1; do { res = curl_multi_perform(m_curl, &ret); } while(ret); handle_curl_error(res, (char*)__FUNCTION__); for(i=0; i<pipeline_req; i++){ curl_multi_remove_handle(m_curl, curl[i]); curl_easy_cleanup(curl[i]); } curl_multi_cleanup(m_curl); return 0; } |
root@Imperfecto_maximuS> gcc send_pipelined_http.c -lcurl -o send_pipelined_http root@Imperfecto_maximuS> ./send_pipelined_http Error: Insufficient arguments... Exiting... Developed by Ahamed(ahamed.en@gmail.com) using cURL API via libcurl Usage : ./send_pipelined_http <no of req> <http://x.x.x.x> Pipeline Request - Min 1 : Max : 32 Hostname not supported yet. Please provide IP address. URL - Min Length 14 : Max Length : 32 root@Imperfecto_maximuS> ./send_pipelined_http 3 http://11.1.1.99 <html><body><h1>This is sample page</h1>\0<p>for testing created...</p></body></html> <html><body><h1>This is sample page</h1>\0<p>for testing created...</p></body></html> <html><body><h1>This is sample page</h1>\0<p>for testing created...</p></body></html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include "stdio.h" #include "limits.h" static unsigned short is_set(unsigned int x, unsigned short bit_pos) { return !!((1<<bit_pos) & x); } int main(int argc, char **argv) { /* 0x1000 = 0000 0000 0000 0000 0001 0000 0000 0000 | 12th Bit */ printf("0x%08x - %d \n", 0x1000, is_set(0x1000, 12)); printf("0x%08x - %d \n", 0x1000, is_set(0x1000, 13)); return 0; } |
[root@Imperfecto_1 ~]# gcc -g run.c && ./a.out
0x00001000 - 1
0x00001000 - 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include "stdio.h" #include "limits.h" /* This creates a 32 bit value with only MSB set * 1 << 31 times produces 10000000000000000000000000000000 i.e. 0x80000000 * And then we AND it with x which will either yield 0x80000000 or 0x0 * !!(0x80000000) = 1 and !!(0x0) = 0 */ static unsigned short is_set_MSB(unsigned int x) { return !!((1<<(sizeof(unsigned int) * CHAR_BIT - 1)) & x); } /* This is straight forward, x & 1 will either yield 1 or 0 directly. */ static unsigned short is_set_LSB(int x) { return (x & 1); } int main(int argc, char **argv) { printf("%08x - %d \n", 0xffffffff, is_set_LSB(0xffffffff)); printf("%08x - %d \n", 0xfffffff0, is_set_LSB(0xfffffff0)); printf("%08x - %d \n", 0xffffffff, is_set_MSB(0xffffffff)); printf("%08x - %d \n", 0x0fffffff, is_set_MSB(0x0fffffff)); return 0; } |
[root@Imperfecto_1 ~]# gcc -g run.c && ./a.out
ffffffff - 1
fffffff0 - 0
ffffffff - 1
0fffffff - 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include "stdio.h" static void swap(int *x, int *y) { *x = *x + *y; *y = *x - *y; *x = *x - *y; return; } static void swap_xor(int *x, int *y) { *x = *x ^ *y; *y = *x ^ *y; *x = *x ^ *y; return; } int main(int argc, char **argv) { int x = 10, y = 20; printf("1> %d %d\n", x, y); swap(&x, &y); printf("2> %d %d\n", x, y); swap_xor(&x, &y); printf("3> %d %d\n", x, y); return 0; } |
[root@Imperfecto_1 ~]# gcc -g run.c && ./a.out
1> 10 20
2> 20 10
3> 10 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include "stdio.h" #include "stdlib.h" #include "string.h" #include "limits.h" /* This function prints the bits in a recursive mode * It takes out the bits from right side i.e. 0, 1, 2 * and prints, this being recursive will print the * output in reverse order which will be finally * the correct order */ static void recurse_bits(int x, int *size) { int bit = x & 0x1; if((*size)--){ recurse_bits(x>>=1, size); printf("%d ", bit); } } /* This function prints the bit by taking it from the left * side, i.e. 31, 30 etc and printing it right away */ static void print_bits(unsigned int x, int *size) { unsigned short bits = *size; unsigned int mask = 0x1 << (bits-1); while(bits--){ printf("%d ", !!(x & mask)); x<<=1; } return; } int main(int argc, char **argv) { int size = sizeof(unsigned int) * CHAR_BIT; int val = 9; print_bits(val, &size); printf("\n"); recurse_bits(val, &size); printf("\n"); return 0; } |
[root@Imperfecto_1 ~]# gcc -g run.c && ./a.out
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
root@Imperfecto_:~# lshw
imperfecto_
description: Desktop Computer
product: Parrot ()
vendor: GOOGLE
version: 1.0
serial: 123456789
width: 64 bits
capabilities: smbios-2.7 dmi-2.7 vsyscall32
configuration: boot=normal chassis=desktop
*-core
description: Motherboard
physical id: 0
and so on...
root@Imperfecto_:~# lshw -C network
*-network
description: Wireless interface
product: AR9462 Wireless Network Adapter
vendor: Atheros Communications Inc.
physical id: 0
bus info: pci@0000:01:00.0
logical name: wlan0
version: 01
and so on...